将多个工作表保存到单个pdf? [英] Saving multiple worksheets to a single pdf?
问题描述
Sub Test()
Application.CutCopyMode = False
For每个sht在ActiveWorkbook.Worksheets
如果InStr(1,sht.Name,STRINGY)> 0然后
表单(sht.Name)。选择替换:= False
结束如果
下一步sht
Selection.ExportAsFixedFormat类型:= xlTypePDF,_
文件名:= C:\File.pdf
任何帮助将不胜感激。
您的方向正确,但没有正确使用 Selection
。以下应该做你所需要的:
Option Explicit
Sub Test()
Dim arrSheets()As String
Dim sht As Worksheet
Dim i As Integer
i = 0
对于每个sht在ActiveWorkbook.Worksheets
如果InStr(1,sht.Name,STRINGY)> 0然后
ReDim保存arrSheets(i)
arrSheets(i)= sht.Name
i = i + 1
End If
Next sht
ThisWorkbook.Sheets(arrSheets)。选择
ActiveSheet.ExportAsFixedFormat类型:= xlTypePDF,_
文件名:=C:\File.pdf,_
OpenAfterPublish := True
End Sub
基本上,您需要添加工作表其名称在数组中包含搜索字符串,并同时选择它们。然后使用该选择作为导出为PDF的基础。
我添加了 OpenAfterPublish
导出选项,以便导出后在PDF阅读器中打开文件;这不是一个要求。
上述代码已经通过Excel 2010成功测试,但无法确定它可以在Excel 2007或以前的版本中工作。请注意,如果当前未安装PDF加载项,则会出现错误。如果是这样,可以在 http://www.microsoft.com/downloads/details.aspx?familyid=4d951911-3e7e-4ae6-b059-a2e79ed87041 。
I am trying to extract a selection of worksheets from a workbook; worksheets whose names contain the string: "STRINGY", and export those worksheets as one single pdf. The code below creates a blank pdf.
Sub Test()
Application.CutCopyMode = False
For Each sht In ActiveWorkbook.Worksheets
If InStr(1, sht.Name, "STRINGY") > 0 Then
Sheets(sht.Name).Select Replace:=False
End If
Next sht
Selection.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\File.pdf"
Any help would be greatly appreciated.
You were along the right lines, but didn't use the Selection
correctly. The following should do what you need:
Option Explicit
Sub Test()
Dim arrSheets() As String
Dim sht As Worksheet
Dim i As Integer
i = 0
For Each sht In ActiveWorkbook.Worksheets
If InStr(1, sht.Name, "STRINGY") > 0 Then
ReDim Preserve arrSheets(i)
arrSheets(i) = sht.Name
i = i + 1
End If
Next sht
ThisWorkbook.Sheets(arrSheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\File.pdf", _
OpenAfterPublish:=True
End Sub
Basically, you need to add the sheets whose name contain your search string in an array and select them all at the same time. Then use that selection as the basis for the export to PDF.
I added the OpenAfterPublish
export option so that the file opens in your PDF reader after export; it's not a requirement.
The above code has been successfully tested with Excel 2010, but can't be certain it'll work in Excel 2007 or previous. Notice that an error will occur if the PDF add-in is not currently installed. If that's the case, you can find it at http://www.microsoft.com/downloads/details.aspx?familyid=4d951911-3e7e-4ae6-b059-a2e79ed87041.
这篇关于将多个工作表保存到单个pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!