Excel VBA ExportAsFixedFormat 不打印边距

三亚

我有一个简单的 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$1:$L$51"
  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
HTH

TopMargin属性接受点,而不是英寸

所以你必须“翻译”英寸为点

TopMargin = Application.InchesToPoints(0.25)

这同样适用于其他边距属性

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章