我在 Internet 上找到了这种方法,并认为它可以在我的代码中使用,因为我有很多页面要格式化并且需要大量时间:
Public Sub PageSetupXL4M(Optional LeftHead As String, Optional CenterHead As String, Optional RightHead As String, _
Optional LeftFoot As String, Optional CenterFoot As String, Optional RightFoot As String, Optional LeftMarginInches As String, _
Optional RightMarginInches As String, Optional TopMarginInches As String, Optional BottomMarginInches As String, _
Optional HeaderMarginInches As String, Optional FooterMarginInches As String, Optional PrintHeadings As String, _
Optional PrintGridlines As String, Optional PrintComments As String, Optional PrintQuality As String, Optional CenterHorizontally As String, _
Optional CenterVertically As String, Optional Orientation As String, Optional Draft As String, Optional PaperSize As String, _
Optional FirstPageNumber As String, Optional Order As String, Optional BlackAndWhite As String, Optional Zoom As String)
Const c As String = ","
Dim pgSetup As String
Dim head As String
Dim foot As String
If LeftHead <> "" Then head = "&L" & LeftHead
If CenterHead <> "" Then head = head & "&C" & CenterHead
If RightHead <> "" Then head = head & "&R" & RightHead
If Not head = "" Then head = """" & head & """"
If LeftFoot <> "" Then foot = "&L" & LeftFoot
If CenterFoot <> "" Then foot = foot & "&C" & CenterFoot
If RightFoot <> "" Then foot = foot & "&R" & RightFoot
If Not foot = "" Then foot = """" & foot & """"
pgSetup = "PAGE.SETUP(" & head & c & foot & c & _
LeftMarginInches & c & RightMarginInches & c & _
TopMarginInches & c & BottomMarginInches & c & _
PrintHeadings & c & PrintGridlines & c & _
CenterHorizontally & c & CenterVertically & c & _
Orientation & c & PaperSize & c & Zoom & c & _
FirstPageNumber & c & Order & c & BlackAndWhite & c & _
PrintQuality & c & HeaderMarginInches & c & _
FooterMarginInches & c & PrintComments & c & Draft & ")"
Application.ExecuteExcel4Macro pgSetup
End Sub
问题是它根本没有格式化页面(也许问题是我使用的是 Excel 2010?)。我在另一段代码中调用 XL4 页面设置:
Dim Sheets_Select
For Each sheet_select In ActiveWorkbook.Worksheets
sheet_select.Select
With sheet_select
.Cells(1, 1).Font.Bold = True
.Cells(1, 1).Value = "type-no. " & TypeNo
.Cells(1, 1).Characters(Start:=1, Length:=17).Font.FontStyle = "Standard"
.Cells(2, 1).Font.Bold = True
.Cells(2, 1).Value = "specification point: " & SpecName
.Cells(2, 1).Characters(Start:=1, Length:=28).Font.FontStyle = "Standard"
End With
With sheet_select
.Rows.RowHeight = 12
.Columns.ColumnWidth = 3.43
.Columns("A:A").ColumnWidth = 8
.Cells.NumberFormat = "0.0"
.Cells.Font.Name = "Arial"
.Cells.Font.Size = 9
.Cells.HorizontalAlignment = xlCenter
.Columns("A:A").HorizontalAlignment = xlLeft
.Rows("1:9").HorizontalAlignment = xlLeft
End With
'###### L o g o ######
sheet_select.Cells(2, 27).Select
sheet_select.Pictures.Insert(apppath + "\Script\logo.bmp").Select
Selection.ShapeRange.ScaleWidth 0.35, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 0.35, msoFalse, msoScaleFromTopLeft
'###### P a g e S e t u p ######
PageSetupXL4M Orientation:="""xlLandscape""", LeftMarginInches:="""0.6""", RightMarginInches:="""0.6""", TopMarginInches:="""0.6""", BottomMarginInches:="""1""", HeaderMarginInches:="""0.5""", FooterMarginInches:="""0.5""", PaperSize:="""xlPaperA4"""
Application.ExecuteExcel4Macro pgSetup
'.LeftHeader = Header(1, 1)
'.CenterHeader = Header(1, 2)
'.RightHeader = Header(1, 3)
'.LeftFooter = Header(2, 1)
'.CenterFooter = Header(2, 2)
'.RightFooter = Header(2, 3)
Next sheet_select