【问题标题】:Excel VBA ExportAsFixedFormat Not Printing MarginsExcel VBA ExportAsFixedFormat 不打印边距
【发布时间】:2020-04-07 14:24:45
【问题描述】:

我有一个简单的 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

【问题讨论】:

    标签: vba pdf margins


    【解决方案1】:

    TopMarginproperty 接受点,而不是英寸

    所以你必须将英寸“转换”为点

    TopMargin = Application.InchesToPoints(0.25)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多