簡體   English   中英

將多個工作表另存為PDF

[英]Save multiple worksheets as PDF

我對使用VBA相當滿意,這是我第一次在該論壇中發布問題,所以如果我違反任何發布規則,請多多包涵。

下面是一段代碼,用於將1個或多個工作表從Excel工作簿打印/保存到PDF。 如果我要導出的每個工作表中的數據都適合單個頁面,則效果很好。 我發現的問題是,如果我的第一張工作表中的數據范圍大於1頁,則僅第一張工作表將被導出,並且只能導出到第一頁。 關於如何解決此問題的任何建議或建議?

'Ask user if report needs to be saved as a pdf
PdfCheck = MsgBox("Save report as PDF", vbYesNo, "Save PDF") 
'use result of <PdfCheck> to control if statement
If PdfCheck = vbYes Then
    Pdfsheets = InputBox("How many worksheets would you like to include in PDF")

    'Prints the number of sheets entered by user
    'starts at first ACTIVE Worksheet to the result of <Pdfsheets>
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    "C:\Users\" & WinName & "\Documents\REPORTING\" & Database & "\" & _
     ReportYear & "\" & FolderMonth & "\" & Database & "-" & Title & "-" & FileDate _
    , Quality:=xlQualityStandard, IncludeDocProperties:=False, _
    IgnorePrintAreas:=False, From:=1, To:=Pdfsheets, OpenAfterPublish:=True
Else
    GoTo X
End If
X:
End Sub

我使用的方法是循環瀏覽工作簿中的工作表,然后選擇符合我的條件的工作表。 然后,我使用ExportAsFixedFormat創建我的pdf。 請注意,您無法選擇隱藏的工作表,因此,如果工作簿包含這些工作表,則需要添加if / then來檢查可見性。

Pdfsheets = InputBox("How many worksheets would you like to include in PDF")
    for x=1 to Pdfsheets
        thisworkbook.sheets(x).select (false) 'use false to select multiple sheets
    next x

'Prints the number of sheets entered by user--starts at first ACTIVE   
Worksheet to the result of <Pdfsheets>
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"C:\Users\" & WinName & "\Documents\REPORTING\" & Database & "\" & ReportYear & "\" & FolderMonth & "\" & Database & "-" & Title & "-" &    FileDate _
, Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM