Excel宏全局子调整

时间:2018-08-16 12:35:27

标签: 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   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

但是,这当然不会作为插件运行,因此我使用了以下文章来创建我认为称为应用程序事件(http://www.cpearson.com/excel/AppEvent.aspx)的内容。本文介绍的过程非常适合示例代码,但是当我尝试使用上面的完整代码时,它将不再起作用。

我知道,当使用应用程序事件引用(例如ThisWorkbook和Sheet)时,它们将不再起作用,因为它们将仅引用加载项而不是活动的工作簿,但我不知道如何移植代码。

如果有人可以修改代码和/或向我解释需要更改的内容以及原因,我将不胜感激。

谢谢! 丹尼尔

0 个答案:

没有答案