VBA-Export as PDF to current(Working) directory? Weird shenanigans...

MikeyW1969

Board Regular
Joined
Apr 28, 2014
Messages
80
Hello,
I am creating a worksheet that I want to export as a pdf when I am finished. I want it to go into the current directory, but it is dropping into "My Documents" in one way and one directory up in the second version. I could get around the thing in the second version, but it is also adding the file name to the final pdf, which I don't want. Here is the two versions of the code I'm using.

Code:
Sub CommitAndSave()
   ActiveSheet.ExportAsFixedFormat _
   Type:=xlTypePDF, _
   Filename:="Invoice" & Range("B5") & "_" & Range("A8"), _
   Quality:=xlQualityStandard, _
   IncludeDocProperties:=False, _
   IgnorePrintAreas:=False, _
   From:=1, _
   To:=5, _
  OpenAfterPublish:=True
End Sub

This version names it correctly, the word Invoice, followed by the Invoice # from the cell where that appears, and the name of the user. But it puts it in the "My Documents" folder, no matter where I am working from.

This is the second version:
Code:
Sub CommitAndSave()
   ActiveSheet.ExportAsFixedFormat _
   Type:=xlTypePDF, _
   Filename:=ThisWorkbook.Path & "Invoice" & Range("B5") & "_" & Range("A8"), _
   Quality:=xlQualityStandard, _
   IncludeDocProperties:=False, _
   IgnorePrintAreas:=False, _
   From:=1, _
   To:=5, _
  OpenAfterPublish:=True
End Sub

This one puts it one level above where I am actually working, which isn't a deal breaker, but it puts the actual original file name out in front of "Invoice", which I don't want. I want to leave the file path as the working location, because I plan to be able to access this anywhere, using Cloud storage, so I can generate these on the fly. I don't want to use a hard coded location on my computer, because that means that I can't generate these unless I am tied down in front of my desk at home.

Any ideas?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I don't know why, but that slash before "Invoice" seems to be the culprit. Thanks!!!

Being inside the quotes, I thought that meant that it would be included in the name as a text object of some kind, but that seems to be the magic key to it all. :-)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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