VBA SAVE AS PDF TO CURRENT DIRECTORY FOLDER WITH CURRENT FOLDER NAME.PDF

hamiveic

New Member
Joined
Mar 29, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

This is my first post.

I'm trying to export the worksheet as PDF.

e.g
The current excel file is saved in path is C:\document\file\ABC
the current folder name is ABC.
B6 = 3

Below code is able to get be to save the PDF in the current folder C:\document\file\ABC as 3.pdf. But it will not save the document as ABC3.pdf.

VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.path & "\" & Range("B6").Value & ".pdf", Quality:=xlQualityStandard, _
  IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True

This will able to get me to save the PDF as ABC3.pdf. But it will save the PDF to the prior folder C:\document\file. Not the sub-folder ABC.

VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.path & Range("B6").Value & ".pdf", Quality:=xlQualityStandard, _
  IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True

What I want to do is to save the PDF in the folder C:\document\file\ABC as ABC3.pdf.

I do not want to set the location permanently to C:\document\file\ABC. Since this is a temple and will be copied and pasted around folders.

Any suggestions? Thank you very much.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this:
VBA Code:
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & Mid(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\")) & Range("B6").Value & ".pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
 
Upvote 0
Solution
Try this:
VBA Code:
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & Mid(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\")) & Range("B6").Value & ".pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
It worked! Thank you so much!
Would you take a min to help explain a bit about this part?
VBA Code:
Mid(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\"))
 
Upvote 0
InStrRev looks backwards to find the position of the last "\" in the Path string. Mid then extracts the Path from that position to the end.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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