"Run-time error '1004'. Document not saved." when exporting to PDF with VBA

sj_robertson

New Member
Joined
Mar 21, 2010
Messages
7
I have a macro that runs from a key combo to save the active worksheet to PDF.

It seems that this macro runs correctly and performs the requested operation (export to PDF), however, it throws up a run-time error, which prevents the macro from completing.

Code:
Sub ...
...
...
..
With ActiveSheet
.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        FileName:="C:\...correct path triple-checked!...\" & Cells(11, 10).Value & "_" & Cells(15, 8).Value & ".pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        OpenAfterPublish:=False
'
End With
'
Sheets("Sheet1").Delete
'
End Sub

VBA repeatedly tells me (highlights during Debug) the offending code starts at .ExportAsFixedFormat and ends after OpenAfterPublish:=False, yet I cannot find anything wrong with the coding, despite having corrected the coding and reverted to my original format numerous times. The only thing I can assume is that the underscore in the filename is causing the issue, yet all resources I have checked specify that underscore is a valid character...

What is even more confusing is that the macro does produce my final PDF (with the file name in the format CellRef_CellRef); it just fails to delete sheet 1 after doing so... If I then, after deleting Sheet1 manually, run the macro again, the macro seems to then run perfectly (overwriting the previously exported file) without throwing up any errors whatsoever.

Any help would be greatly appreciated, as I'm certain I've grown a few more grey hairs since this morning!
 
I have a similar problem with the Run Time Error 1004 -

SharePointPath = "C:\Temp\RBB Test\"

Path = SharePointPath

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Path & PDFFileName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Have you confirmed that the folder RBB Test exists within the Temp folder and that you can manually create a file/folder inside of it?
 
Upvote 0

Forum statistics

Threads
1,215,106
Messages
6,123,124
Members
449,097
Latest member
mlckr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top