【问题标题】:How to print exel to PDF excluding the first sheet如何将excel打印到PDF,不包括第一张纸
【发布时间】:2021-06-22 06:52:35
【问题描述】:

我正在尝试将 Excel 工作簿打印为 pdf,但从 PDF 中排除标题为基本信息的第一张工作表。目前整个工作簿都包含在 PDF 中

Option Explicit

Sub CreatePDF()
  Dim IsCreated As Boolean
  Dim PdfFile As String, Title As String
  Dim s As Worksheet
  Dim DoNotInclude As String

  With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
  End With

  Title = "I&T Plan for " & Worksheets("Basic Information").Range("C7").Value
  PdfFile = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & Title & ".pdf"
  
    Sheets("Front Sheet").Select
   
  
  DoNotInclude = ("Basic Information")
  
    For Each s In ActiveWorkbook.Worksheets
    If s.Visible = True Then
      If InStr(DoNotInclude, s.Name) = 0 Then
          s.Select (False)
      End If
    End If
  Next

  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, fileName:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
  End With

  Sheets("Front Sheet").Select

  MsgBox "Created PDF file on the desktop", vbOKOnly, "I&T PDF"

  With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
  End With


End Sub

【问题讨论】:

  • 有什么问题?它工作正常。
  • 如上所述,基本信息表包含在 PDF 中

标签: excel vba pdf


【解决方案1】:

选择除第一张以外的所有工作表:

Dim x As Long, wb As Workbook
    
Set wb = ActiveWorkbook
wb.Sheets(2).Select
For x = 3 To Sheets.Count
    wb.Sheets(x).Select False
Next x

打印...

【讨论】:

    【解决方案2】:

    将多个工作表导出为 PDF

    • 有趣的事实:如果您选择“基本信息”以外的任何工作表,您的代码将按要求工作。您必须考虑到当前选择的(活动)工作表最终会出现在选择中。

    不关心选择了哪个工作表或工作表的位置(索引),您可以使用:

    Dim arr As Variant: ReDim arr(1 To ActiveWorkbook.Worksheets.Count)
    Dim n As Long
    For Each s In ActiveWorkbook.Worksheets
      If s.Visible = True Then
        If InStr(1, DoNotInclude, s.Name, vbTextCompare) = 0 Then
            n = n + 1
            arr(n) = s.Name
        End If
      End If
    Next
    ReDim Preserve arr(1 To n)
    ActiveWorkbook.Worksheets(arr).Select
    

    字里行间:

    DoNotInclude = "Basic Information"
    

    With ActiveSheet
    

    【讨论】:

    • 谢谢 - 效果很好。有趣的是,代码是从另一个工作簿中复制的,并且工作正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多