Run-time error 1004 when trying to save a sheet as PDF

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
886
Office Version
  1. 365
Platform
  1. Windows
This code works just fine in another spreadsheet I use. All I have to do is set what I want printed (set print area) and then run this code however with a new spreadsheet I have I am getting error 1004 and I am not sure why.

Here is the code, any idea what I could be missing?

The cell references for file name and file locations in Z1 and Z2 are formulas. Is there something I have to do to the code to make it understand/read the formula result value for the directory in Z1?

VBA Code:
Sub SavePDF()
'
' SavePDF Macro
'
Dim Path As String
Dim filename As String

Path = Sheets("Defects by Workstation Table").Range("Z1")
filename = Sheets("Defects by Workstation Table").Range("Z2")

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path & filename & ".pdf", Quality:=xlQualityStandard, _
  IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
'

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I would guess that it isn't creating a valid path.
Also note, is there a "\" at the end of the value in cell Z1?

If not, you will want to amend this part of your formula:
VBA Code:
 filename:=Path & filename & ".pdf"
to
VBA Code:
 filename:=Path & "\" & filename & ".pdf"
 
Upvote 0
Solution
I would guess that it isn't creating a valid path.
Also note, is there a "\" at the end of the value in cell Z1?

If not, you will want to amend this part of your formula:
VBA Code:
 filename:=Path & filename & ".pdf"
to
VBA Code:
 filename:=Path & "\" & filename & ".pdf"

That was it. Thank you very much.

Just needed a 2nd pair of eyes! Thank you again :)
 
Upvote 0
You are welcome.

Since I cannot control whether or not they will put a "\" at the end, I often use this method instead:
VBA Code:
Path = Sheets("Defects by Workstation Table").Range("Z1")
If Right(Path,1)<>"\" Then Path = Path & "\"
Then just keep what you originally had for the filename in your export command.
That will handle either case.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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