I am trying to save a specific sheet as PDF with below code but an error msg is showing Run Time Error'1004': Method 'SaveAs' of object '_Worksheet' Failed
Sub SaveAsPDF()
Dim path As String
Dim MyDate As String
Dim WS As Worksheet
path = "c:\invoice\"
MyDate = Date
MyDate = Format(MyDate, "dd_mm_yyyy")
Application.DisplayAlerts = False
For Each WS In ThisWorkbook.Worksheets
WS.SaveAs Filename:=path & Range("D6").Text & "-" & Range("K6").Value & "-" & _
MyDate, FileFormat:=xlTypePDF
Next
Application.DisplayAlerts = True
ActiveWorkbook.Close savechanges:=False
End Sub
You need to use ExportAsFixedFormat
, not SaveAs
(and the appropriate parameter name)
Sub SaveAsPDF()
Dim path As String
Dim MyDate As String
Dim WS As Worksheet
path = "c:\invoice\"
MyDate = Date
MyDate = Format(MyDate, "dd_mm_yyyy")
Application.DisplayAlerts = False
For Each WS In ThisWorkbook.Worksheets
WS.ExportAsFixedFormat Filename:=path & Range("D6").Text & "-" & Range("K6").Value & "-" & MyDate, Type:=xlTypePDF
Next
Application.DisplayAlerts = True
ActiveWorkbook.Close savechanges:=False
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.