Option Explicit
Sub SaveAsPDF()
Dim FSO As Object
Dim s(1) As String
Dim FilePath As String
Set FSO = CreateObject("Scripting.FileSystemObject")
s(0) = ThisWorkbook.FullName
If FSO.FileExists(s(0)) Then
'// Change Excel Extension to PDF extension in FilePath
s(1) = FSO.GetExtensionName(s(0))
If s(1) <> "" Then
s(1) = "." & s(1)
FilePath = Replace(s(0), s(1), ".pdf")
'// Export to PDF with new File Path
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FilePath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
Else
'// Error: file path not found
MsgBox "Error: This workbook may be unsaved. Please save and try again."
End If
Set FSO = Nothing
End Sub
To Export the workbook Try changing ActiveSheet
To ActiveWorkbook
To Export only multiple sheets selections try using Sheets(Array("Sheet4", "Sheet5"))
Example:
ThisWorkbook.Sheets(Array("Sheet2", "Sheet3")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FilePath, _
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…