Excel VBA ExportAsFixedFormat 不打印边距

Excel VBA ExportAsFixedFormat Not Printing Margins

我有一个简单的 Sub,可以将范围作为 PDF 保存在用户指定的文件夹中。问题是它生成的 PDF 顶部边距为 0。我需要 0.25" 的边距。 我做错了什么?

Private Sub btnPrintJobWorksheet_Click()

   Dim folderPath As String, filePath As String, fileName As String, jobNumber, rng As String    
   Dim ws As Worksheet

  'Get the Job Number and create the File Name
  jobNumber = ThisWorkbook.Names("JOBNUMBER").RefersToRange.Value
  fileName = "Job Worksheet - " & jobNumber & ".pdf"

  'Allow the user to select the folder to save to
  With Application.FileDialog(msoFileDialogFolderPicker)
     If .Show = -1 Then
        folderPath = .SelectedItems(1)
        filePath = folderPath & "\" & fileName
    End If
  End With

  'Retrieve the Print Area
  Set ws = ThisWorkbook.ActiveSheet
  rng = CStr(ws.PageSetup.printArea)

  'Set the Page Margins
  With ws.PageSetup
    .CenterHorizontally = True
    .TopMargin = 0.25
    .RightMargin = 0.2
    .BottomMargin = 0.25
    .LeftMargin = 0.2
    .HeaderMargin = 0.1
    .Zoom = False
    .FitToPagesTall = 1
    .FitToPagesWide = 1
  End With



  'If No Print Area was found, then set the Print Area range to its default value
  If (Len(rng) < 2) Then
      rng = "$B:$L"
  End If

  'If we have a File Path and we have a range, then save the PDF
  If Len(filePath) > 0 And Len(rng) > 2 Then
    ws.Range(rng).ExportAsFixedFormat _
        Type:=xlTypePDF, fileName:=filePath, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, openAfterPublish:=True
  End If

  Set ws = Nothing

End Sub

TopMarginproperty 接受点,而不是英寸

所以你必须 "translate" 英寸到点

TopMargin = Application.InchesToPoints(0.25)

同样适用于其他边距属性