VBA creating pdf files and saving them in the same folder where the workbook is saved

hyunee

New Member
Joined
Sep 21, 2016
Messages
12
Hello everyone

I am trying to create individual pdf file for three active sheets (Report1, Report2 and Report3) and want to save them in the same folder as where the current excel workbook is saved. i have codes below and the pdf files are created okay but they are being saved in different folder. i cant figure it out why and how to fix it. can you please help?
I have saved the workbook and closed and re-opened it and the pdf files are still saved in the wrong place....
Thank you in advance!

*here are some background which might help you to help me...
i normally create the following month workbook by changing file name of previous month excel workbook and created a new folder for the month i am working and saved them. i want the pdf files to be saved in the THAT folder i created not previous folder!


Codes that i have at the moment are...

Sheets("Report1").Select
Dpath = "Report1" & Format(Range("h7"), "mmmm yyyy")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Dpath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True

Sheets("report2").Select
Dpath = "Report2" & Format(Range("h7"), "mmmm yyyy")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Dpath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True

Sheets("report3").Select
Dpath = "Report3 " & Format(Range("h7"), "mmmm yyyy")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Dpath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You would get better results if you actually supplied the path. Try:
Code:
Sub PDF_Save()
Dim i As Long, StrPath As String, StrFlNm As String
StrPath = ActiveWorkbook.Path & "\"
For i = 1 To 3
  With Sheets("Report" & i)
    StrFlNm = "Report" & i & Format(.Range("H7"), " mmmm yyyy")
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=StrPath & StrFlNm, _
      Quality:=xlQualityStandard, IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, OpenAfterPublish:=True
  End With
Next
End Sub
 
Upvote 0
Thank you!!

The problem is the tab name(worksheet name) are not actually Report 1,2 and 3. i just changed it to 1,2,3 for you to understand easy. The worksheet names are product names like Cola, Pepsi, and Fanta so your coding below wont work.... sorry can you please please give me different codes?
 
Upvote 0
It would have been helpful had you said they weren't the real worksheet names instead of having me waste time providing a streamlined solution that doesn't apply... As it is, the only part of my coding that won't work is the loop You will instead need to replace the references to:
"Report" & I
with whatever the real worksheet names are and have a sequence of such With - End With constructs - or you could adapt the code I posted, thus:
Code:
Sub PDF_Save()
Dim i As Long, StrPath As String, StrFlNm As String, ArrNames
ArrNames = Array("Cola", "Pepsi", "Fanta")
StrPath = ActiveWorkbook.Path & "\"
For i = 0 To UBound(ArrNames)
  With Sheets(ArrNames(i))
    StrFlNm = Sheets(ArrNames(i)) & Format(.Range("H7"), " mmmm yyyy")
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=StrPath & StrFlNm, _
      Quality:=xlQualityStandard, IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, OpenAfterPublish:=True
  End With
Next
End Sub
 
Upvote 0
Thank you! and sorry for not telling you the sheet name were not real name before.

I have changed the codes as below and it still not working. It comes up as Complie error. Sorry i am new to VBA and only started learning recently. If you could help one more time please...

also i am not sure why the pdf is not being saved automatically in the same folder where the excel book is saved when i dont have any specific path in the coding..



Sub PDF_Save()
Dim i As Long, StrPath As String, StrFlNm As String
StrPath = ActiveWorkbook.Path & ""
For i = 1 To 3
With Sheets("Cola", "Pepsi", "Fanta")
StrFlNm = "Cola" & Format(.Range("H7"), " mmmm yyyy")
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=StrPath & StrFlNm, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End With
Next



End Sub
 
Last edited:
Upvote 0
I have, but it didnt work either. it keeps saying " Object doesn't support this property or method.

Is there anyway i could send you the excel workings with the codes i have? i literally copied and pasted your codes and not sure why it's not working..
 
Upvote 0
Typo. Try:
Code:
Sub PDF_Save()
Dim i As Long, StrPath As String, StrFlNm As String, ArrNames
ArrNames = Array("Cola", "Pepsi", "Fanta")
StrPath = ActiveWorkbook.Path & "\"
For i = 0 To UBound(ArrNames)
  With Sheets(ArrNames(i))
    StrFlNm = ArrNames(i) & Format(.Range("H7"), " mmmm yyyy")
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=StrPath & StrFlNm, _
      Quality:=xlQualityStandard, IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, OpenAfterPublish:=True
  End With
Next
End Sub
 
Upvote 0
It's now working! Thank you so much! i really appreciate your help!!:biggrin:
Cant wait to get better at VBA!
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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