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

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
716
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
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,742
Office Version
  1. 365
Platform
  1. Windows
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"
 
Solution

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
716
Office Version
  1. 365
Platform
  1. Windows
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 :)
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,742
Office Version
  1. 365
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,107
Messages
5,628,729
Members
416,333
Latest member
Time2Learn

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
Top