Excel宏全局分调整
我正在尝试创建一个加载项,当我公司的用户打开工作簿名称包含字符串“en-us”的文件时,该加载项将自动打印格式(打印区域和一些设置)。如果在示例报告中作为子项插入宏,宏本身运行良好。代码如下:Excel宏全局分调整,excel,vba,global,add-in,Excel,Vba,Global,Add In,我正在尝试创建一个加载项,当我公司的用户打开工作簿名称包含字符串“en-us”的文件时,该加载项将自动打印格式(打印区域和一些设置)。如果在示例报告中作为子项插入宏,宏本身运行良好。代码如下: Private Sub App_WorkbookOpen(ByVal Wb As Workbook) If Wb.Name Like "*en-us*" Then Dim LastRow As Long Dim LastCol As Long Dim myRng
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
If Wb.Name Like "*en-us*" Then
Dim LastRow As Long
Dim LastCol As Long
Dim myRng As Range
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastCol = WorksheetFunction.Max(.Cells(1, .Columns.Count).End(xlToLeft).Column, _
.Cells(2, .Columns.Count).End(xlToLeft).Column, .Cells(3, .Columns.Count).End(xlToLeft).Column, _
.Cells(4, .Columns.Count).End(xlToLeft).Column, .Cells(5, .Columns.Count).End(xlToLeft).Column, _
.Cells(6, .Columns.Count).End(xlToLeft).Column, .Cells(7, .Columns.Count).End(xlToLeft).Column, _
.Cells(8, .Columns.Count).End(xlToLeft).Column, .Cells(9, .Columns.Count).End(xlToLeft).Column, _
.Cells(10, .Columns.Count).End(xlToLeft).Column, .Cells(11, .Columns.Count).End(xlToLeft).Column, _
.Cells(12, .Columns.Count).End(xlToLeft).Column, .Cells(13, .Columns.Count).End(xlToLeft).Column, _
.Cells(14, .Columns.Count).End(xlToLeft).Column, .Cells(15, .Columns.Count).End(xlToLeft).Column, _
.Cells(16, .Columns.Count).End(xlToLeft).Column, .Cells(17, .Columns.Count).End(xlToLeft).Column, _
.Cells(18, .Columns.Count).End(xlToLeft).Column, .Cells(19, .Columns.Count).End(xlToLeft).Column, _
.Cells(20, .Columns.Count).End(xlToLeft).Column, .Cells(21, .Columns.Count).End(xlToLeft).Column, _
.Cells(22, .Columns.Count).End(xlToLeft).Column, .Cells(23, .Columns.Count).End(xlToLeft).Column, _
.Cells(24, .Columns.Count).End(xlToLeft).Column, .Cells(25, .Columns.Count).End(xlToLeft).Column)
Set myRng = .Range("A1", .Cells(LastRow, LastCol))
.PageSetup.PrintArea = myRng.Address(external:=True)
End With
Next ws
Sheets.Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
.HeaderMargin = Application.InchesToPoints(0.25)
.FooterMargin = Application.InchesToPoints(0.25)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 0
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
Sheets(1).Select
Else: MsgBox ("Not an Proph+IT Model")
End If
End Sub
但是,这当然不会作为外接程序运行,因此我使用下面的文章创建了一个我认为被称为应用程序事件()的东西。本文所解释的过程对于示例代码非常有效,但是当我尝试使用上面的完整代码时,它就不再有效了
我知道,当使用诸如ThisWorkbook和Sheet之类的应用程序事件引用时,它们不再有效,因为它们只会引用加载项而不是活动工作簿,但我不知道如何移植代码
如果有人能修改代码和/或向我解释需要更改的内容和原因,我将不胜感激
谢谢!
Daniel为代码的开头和结尾在blockqoute之外表示歉意,编辑器很难使用。当您在名称为“en-us”的工作簿上测试它时,它是否出错或只是没有运行?请使用事件传递的
Wb
参数。。。