Macro to save as PDF with auto filename as cell value - gives Compile error - Syntax error

Brass Contributor

I have previously used a macro to call PDF Creator to save an invoice created in excel 365. However, it uses the old version of PDF Creator, because the new one has totally different methodology, names and calls.

Since the last update of Windows, whenever I call that macro, the print spooler hangs and freezes excel, so I am looking for a more up to date method that does not use an external programme.

 

I found "Macro to save as PDF with auto filename as cell value" https://answers.microsoft.com/en-us/msoffice/forum/all/macro-to-save-as-pdf-with-auto-filename-as-ce... from @Garys Student 

But it does not work. It gives an error:

"Microsoft Visual Basic for Applications!

Compile error:

Syntax error"

2019 11 01 Excel VBA error.png

 

 

 

Sub MacroPDF_Excel()

s = Range("C56").Value
'
      ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            s,  Quality:=xlQualityStandard, IncludeDocProperties _
            :=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub

 

 

 

The macro is a direct copy and paste from the example that others have made work.

 

I am getting the filename by pulling together various cells containing an invoice number and a date. The file location is OneDrive. This is the exact same filename and location that works in my old macro with PDF Creator v1

 

The cell content value shown in my spreadsheet in Cell C56 is this:

C:\Users\me\OneDrive\Folder\SubFolder\Sub-Sub_Folder\1403 Invoice 2019-10-31 from MyBusiness.PDF

I have checked that putting 

C:\Users\me\OneDrive\Folder\SubFolder\Sub-Sub_Folder\

into Windows File Explorer does take me to the correct folder. 

I've tried it with the simple filename of "fred.pdf" in cell C56 instead of the complex one.

 

Am I doing something wrong or has the method changed since those examples?

1 Reply
Well!

I posted elsewhere, and got a reply. Someone had copied and pasted my code from the post and it worked for them.

So I copied and pasted my code from the post into my macro, and it worked.

But the code in my post had been copied and pasted from my original macro, where it didn't work.

So the exact same text, copied and pasted twice, worked when the original didn't.

To mess up is human. To really muck it up beyond belief requires a computer.