I have tried using the following solution to print from Excel to PDF:
Excel 2013 Print to PDF in VBA
While the solution seems to have worked for other people
If the worksheet that you are exporting contains no data then the export fails. I found this solution on the MrExcel forum, where someone appears to have stumbled over the answer!
OK, I just played with mine some more, and if I take the ".pdf" off the filename specification, I don't get the error 1004.
I had to remove line breaks in my filename's variable using the clean function:
xlName = Range("CustomerName").Value xlName = Application.WorksheetFunction.Clean(xlName)
Another cause of this error is if the filename contains illegal characters such as:
See 'Naming conventions' here: https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
Solution: replace illegal characters with legal ones, such as '_'.
So this is the working code now for Windows users (Mac OS might have to adjust file path):
Sub Invoice_to_PDF()
'Saves the invoice print area to a PDF file
Dim fp As String
Dim wb As Workbook
Dim ws As Worksheet
fp = "C:\Users\[username]\Desktop\NewInvoice.pdf"
Set wb = ActiveWorkbook
Set ws = Worksheets("Invoice")
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fp, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub