save PDF file into sub-folder

GedSalter

Board Regular
Joined
Apr 24, 2019
Messages
80
I have a macro to save a worksheet and protect it. It then also saves the new worksheet as a PDF. I want to save the PDF into a particular subfolder called "Invoices" The sub folder will always be in the main folder. I cant use a spefic path at the main folder needs to work on different computers. this is what I have so far.

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub AddSheet()[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Dim ws As Worksheet
Dim wh As Worksheet
Set ws = Worksheets(ActiveSheet.Name)
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
Set wh = Worksheets(Sheets.Count)
If ws.Range("e9").Value <> "" Then
wh.Name = ws.Range("E9").Value
ActiveSheet.Protect
End If
wh.Activate
Range("A1").Select

ChDir ActiveWorkbook \ Invoices

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("E9")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("E9"), _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif][/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] End Sub
[/FONT]


It all works except for the location of the PDF file. If I remover the following line

ChDir ActiveWorkbook \ Invoices

it saves in the same folder as the excel file. But I want it into a subfolder called Invoices which is located in the same folder.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,

I think from what you are saying is that the macro workbook is in the main folder?
If so you can use ActiveWorkbook.Path


Code:
Sub AddSheet()
Dim ws As Worksheet
 Dim wh As Worksheet
 Set ws = Worksheets(ActiveSheet.Name)
 ActiveSheet.Copy After:=Worksheets(Sheets.Count)
 Set wh = Worksheets(Sheets.Count)
 If ws.Range("e9").Value <> "" Then
 wh.Name = ws.Range("E9").Value
 ActiveSheet.Protect
 End If
 wh.Activate
 Range("A1").Select


[COLOR="#FF0000"]strPath = ActiveWorkbook.Path & "\Invoices\"[/COLOR]

 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=[COLOR="#FF0000"]strPath & [/COLOR]Range("E9"), _
 Quality:=xlQualityStandard, IncludeDocProperties:=True, _
 IgnorePrintAreas:=False, OpenAfterPublish:=False

 End Sub
 
Last edited:
Upvote 0
sorry Dave but not working.

Its saving the PDF file in the same location as workbook and not in the folder Invoices,



Ged
 
Upvote 0
It worked for me. Try this and see what path comes up in the message box

Code:
Sub AddSheet()
    Dim ws As Worksheet
    Dim wh As Worksheet
    Set ws = Worksheets(ActiveSheet.Name)
    ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    Set wh = Worksheets(Sheets.Count)
    If ws.Range("e9").Value <> "" Then
        wh.Name = ws.Range("E9").Value
        ActiveSheet.Protect
    End If
    wh.Activate
    Range("A1").Select




    strpath = ActiveWorkbook.Path & "\Invoices\"
[COLOR=#ff0000]    MsgBox strpath[/COLOR]
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strpath & Range("E9"), _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

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