我尝试了几种方法来包括选择工作表,然后导出为pdf。每次只有第一张纸被证明包含在pdf文件中。我要达到的目的是:1.手动选择工作表。2.运行宏。该宏将执行以下操作:1.将选定的工作表导出为单个pdf。2.取消选择选定的图纸(以免以后在选定的图纸上无意间进行编辑。)
Sub Export_Selected_Sheets_To_PDF()
Dim ws As Worksheet
Dim PDF_Name, Doc_ID, Excel_Name, SelectedSheets() As String
Dim n, i As Long
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'Code for naming the PDF (equals excel filename including a prefix from a cell named "DocID")
'Will be saved at same location as the excel-file
Doc_ID = Range("DocID").Value 'DocID = named cell in the WS
Excel_Name = fso.GetBaseName(ActiveWorkbook.FullName) 'Excel filename without file extension
PDF_Name = Doc_ID & "_" & Excel_Name & ".pdf" 'PDF filename
'Showing the PDF_Name
Debug.Print PDF_Name
'Code for selecting the "selected" sheets
n = 0
For Each ws In ActiveWindow.SelectedSheets
ReDim Preserve SelectedSheets(n)
SelectedSheets(n) = ws.Name
n = n + 1
Next
'Showing the list of selected sheets
For i = LBound(SelectedSheets) To UBound(SelectedSheets)
Debug.Print SelectedSheets(i)
Next i
'Export selected sheets to pdf
Sheets(SelectedSheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDF_Name, Quality:= _
xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
'Deactivate selection to avoid editing across selected sheets.
ActiveSheet.Select
End Sub
在Excel VBA中,提出了将所选图纸导出为PDF的方法,该方法与您使用的方法相同。
但是,进一步的测试表明,此技术取决于在每个工作表上选择的单元格组。
然后将代码修改为:
Sub Macro1()
Sheets("Sheet1").Activate
ActiveSheet.UsedRange.Select
Sheets("Sheet2").Activate
ActiveSheet.UsedRange.Select
Sheets("Sheet3").Activate
ActiveSheet.UsedRange.Select
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\James\Desktop\pdfmaker.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
该答案被强烈推荐,因此可能对您有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句