I have a problem exporting one Excel sheet in a workbook into a PDF file.
Sub ExportPDF() Dim Nazwa As String ' Creating a destination folder for PDF file If Len(Dir("c:\Faktury", vbDirectory)) = 0 Then MkDir "c:\Faktury" End If 'InputBox that allows user to type filename Nazwa = InputBox("Wpisz nazwę pliku", "Nazwa pliku", Sheets("Fa VAT").Range("G3")) If Nazwa = "" Then Exit Sub ChDir "c:\Faktury" 'Exporting sheet "Fa VAT" to PDF Sheets("Fa VAT").ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=Nazwa, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=True End Sub
I get the error “Run-time error ‘5’: Invalid procedure call or argument”. After that Excel highlights the last part of the code as it is supposed to be wrong. I mean exactly this part:
Sheets("Fa VAT").ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=Nazwa, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=True
I installed Microsoft SaveAsPDF setup and nothing changes. I changed filename to some random string rather than a referring to InputBox. I tried to activate the right sheet and then export ActiveSheet. I tried with the sheet hidden and shown. I tried to run the file on two other computers.
When I copy this macro to some completely new, blank excel spreadsheet, it works.
I work on Excel 2007, Windows 10. I tried it also on Excel 2010 and Windows 7.
Here are some of the links that I tried:
Make sure the worksheet isn’t hidden. Excel will throw an error for .ExportasFixedFormat if the worksheet is hidden or very hidden. If yours is hidden, you can unhide it first in your code:
Sheets(“Fa VAT”).Visible = xlSheetVisible
At the end, you can hide it again.
Sheets(“Fa VAT”).Visible = xlSheetHidden
I had the same error. In my case, the filepath and name given were too long. Shorten the name or path and it should work.