将创build多个PDF的VBA更改为单个PDF

下面的代码非常适合从工作表3创build名为“发布”的工作表的PDF,而忽略任何隐藏的工作表。 它为每个这些工作表创build一个单独的PDF。 这是链接到一个形状,用户单击,然后提示select一个文件夹来保存所有的PDF文件。

我试图改变下面的代码做完全相同的事情,除了在工作表3和“发布”之间为每个可见的工作表创build一个PDF。

我已经按摩了一段时间的代码,想知道是否有人知道最好的方法来完成这个?

Sub SaveAllPDF() Dim i As Integer Dim Fname As String Dim TabCount As Long TabCount = Sheets("Post").Index 'Set the TabCount to the last cell you want to PDF Dim dialog As FileDialog Dim path As String Set dialog = Application.FileDialog(msoFileDialogFolderPicker) dialog.AllowMultiSelect = False If dialog.Show = -1 Then path = dialog.SelectedItems(1) ' Begin the loop. For i = 3 To TabCount 'Set i = the number of the first sheet you want to PDF in order from left to right To TabCount If Sheets(i).Visible <> xlSheetVisible Then Else With Sheets(i) Fname = .Range("C15") & " " & .Range("E13") & "-" & .Range("B1") 'The Fname above is equaling the cells that the PDF's filename will be 'The folder directory below is where the PDF files will be saved .ExportAsFixedFormat Type:=xlTypePDF, FileName:=path & "\" & Fname, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End With End If Next i Call Shell("explorer.exe" & " " & path & "\", vbNormalFocus) 'This opens the folder where the PDFs are saved End If End Sub 

如果您用鼠标select多个工作表选项卡,然后select打印,它将打印所有作为一个打印作业,所以试一试代码:

 Sub SaveAllPDF() Dim i As Integer Dim Fname As String Dim TabCount As Long Dim aSheetnames As Variant TabCount = Sheets("Post").Index 'Set the TabCount to the last cell you want to PDF Dim dialog As FileDialog Dim path As String Set dialog = Application.FileDialog(msoFileDialogFolderPicker) dialog.AllowMultiSelect = False If dialog.Show = -1 Then path = dialog.SelectedItems(1) ' Begin the loop. For i = 3 To TabCount 'Set i = the number of the first sheet you want to PDF in order from left to right To TabCount If Sheets(i).Visible <> xlSheetVisible Then Else redim preserve aSheetnames(i-2) 'subtract 2, since i starts at 3 asheetnames(i-2) = sheets(i).name 'build array of the sheets to print End If Next Fname = 'make something up here for your bulk file name Sheets(asheetnames).ExportAsFixedFormat Type:=xlTypePDF, FileName:=path & "\" & Fname, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True End If End Sub 

:没有保certificate示或暗示,你可能需要做一些debugging,因为这是我的头顶,但它可能工作…

这在我的文件中用于通过电子邮件发送可见标签作为PDF的使用,而不同的相同的情况适用…你不需要编码隐藏/不隐藏与此

 ' Export activesheet as PDF With ActiveWorkbook .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End With 

不要在循环导出每个工作表,而是使用Workbook.ExportAsFixedFormat将整个工作簿导出到循环外部

使用这种方法,参数FromTo允许您select要输出的页面。 但是,您需要知道可见的工作表页码所在的位置。 可能不一定是按照他们的工作表编号,因为一些工作表可以打印到多个页面。 通过手动将整个工作簿保存为PDF来查找页码。

或者,您可以继续循环,并使用Adobe Acrobat SDK合并多个PDF。 请参阅AcroExch.AvDocAcroExch.PDDoc对象。 但是,Excel工作簿的用户需要在他们的机器上安装Adobe Acrobat(而不仅仅是免费的Reader)才能引用VBA中的Adobe API。