VBA/Macro to save as a pdf with a designated path and file name

youracct

New Member
Joined
Feb 21, 2018
Messages
10
I am extremely new to this, but this is what I have right now. This changes the name of the invoice based on a cell value. Is it possible to also change the path based on a cell value? (the bold area)


Sub Button1_Click()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Receivable\Invoices\Coco" & Range("K9").Text & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, 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.
Assuming K8 contains the name of the subfolder (Coco in this case):
Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Receivable\Invoices\" & Range("K8").Text & "\" & Range("K9").Text & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
 
Upvote 0
So this appeared to be working but when that cell value changes it will not save to that file name and throws up a debug error. It only works with one value. Any idea on why it is doing that?
 
Upvote 0
The cell value is referencing a pivot table with a vendor name in it. This link only works with one of the vendors. It is giving me a Run-time error '1004' document not saved. the document may be open, or an error may have been encountered when saving. Then it gives me the option to end or debug. The error does not go away when I physically type a different vendor name. I also double-checked that the file names were correct for the other vendors.
 
Upvote 0
Are the K8 and K9 values valid folder and file names?

Does the K8 subfolder exist? You would get that error if it doesn't. Insert this as the first line:
Code:
    If Dir("C:\Receivable\Invoices\" & Range("K8").Text) = vbNullString Then MkDir "C:\Receivable\Invoices\" & Range("K8").Text
 
Upvote 0
I cannot get this code to work I am not sure if I am doing it right, I am getting a compile error now.



Sub Button2_Click()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=_
If Dir("C:\Receivable\Invoices" & Range("C88").Text) = vbNullString Then MkDir "C:\Receivable\Invoices" & Range("C88").Text" & Range("H82").Text & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=
True

End Sub

I checked the file names for all the vendors and they are all there.
 
Upvote 0
So, I think I figured it out. The data that the pivot table was pulling from apparently had a space after the vendor name. :/ user error on my part. Bad habit of hitting the space bar.
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,313
Members
453,032
Latest member
Pauh

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