Print to PDF into created folder in Workbook location

camerong

New Member
Joined
May 9, 2023
Messages
42
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

Need help on this one please, not sure how to go about doing it. I have the code below that I have been using up until now which prints the active sheet to PDF in the Downloads folder.

What I would like it to do is to do the following, in sequence:

1. Create a new folder in the location of the Workbook. Name this folder based on the contents of worksheet cells "B4" and "B5", then the current date and time.
Name the folder in this order: "B4"" - ""B5"" - ""dd.mm.yyyy hh:mm:ss"

2. Inside this newly created folder, run the code below so that the PDF is generated inside.

Thanks for the help guys


VBA Code:
Sub Print_to_PDF()
  '
  ' PDF_Single_Page Macro
  ' Saves to PDF Ctrl+Shft+End page selection area
  '
  RFIPrefix = "RFI "
  RFINum = Range("E4") & " - "
  JobNum = Range("B4") & " - "
  JobName = Range("B5")
  Exten = ".pdf"
  '
  
  With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0)
    .RightMargin = Application.InchesToPoints(0)
    .TopMargin = Application.InchesToPoints(0)
    .BottomMargin = Application.InchesToPoints(0)
    .HeaderMargin = Application.InchesToPoints(0)
    .FooterMargin = Application.InchesToPoints(0)
    .CenterHorizontally = True
    .CenterVertically = False
    .PaperSize = xlPaperA4
    .BlackAndWhite = False
    .FitToPagesWide = 1
    .FitToPagesTall = False
  End With
  Application.PrintCommunication = True
  
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="C:\Users\" & Environ("Username") & "\Downloads\" & RFIPrefix & RFINum & JobNum & JobName & Exten, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=False
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi.

Some notes:
1)

1. Create a new folder in the location of the Workbook.
Does that mean create the folder in the folder where you have the file with the macro?
If so, then that's what this line is for:
VBA Code:
  newFolder = ThisWorkbook.Path & "\" & JobNum & JobName & " - " & Format(Now, "dd.mm.yyyy hh.mm.ss")

2)
You can't use a colon ":" for the folder name, so I used a dot "."

----- --
Try this:
VBA Code:
Sub Print_to_PDF()
  '
  ' PDF_Single_Page Macro
  ' Saves to PDF Ctrl+Shft+End page selection area
  '
  Dim RFIPrefix As String, RFINum As String, JobNum As String, JobName As String, Exten As String
  Dim newFolder As String
 
  RFIPrefix = "RFI "
  RFINum = Range("E4").Value & " - "
  JobNum = Range("B4").Value & " - "
  JobName = Range("B5").Value
  Exten = ".pdf"
  '
  newFolder = ThisWorkbook.Path & "\" & JobNum & JobName & " - " & Format(Now, "dd.mm.yyyy hh.mm.ss")
 
  With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0)
    .RightMargin = Application.InchesToPoints(0)
    .TopMargin = Application.InchesToPoints(0)
    .BottomMargin = Application.InchesToPoints(0)
    .HeaderMargin = Application.InchesToPoints(0)
    .FooterMargin = Application.InchesToPoints(0)
    .CenterHorizontally = True
    .CenterVertically = False
    .PaperSize = xlPaperA4
    .BlackAndWhite = False
    .FitToPagesWide = 1
    .FitToPagesTall = False
  End With
  Application.PrintCommunication = True
 
  If Dir(newFolder, vbDirectory) = "" Then
    MkDir newFolder
  End If
 
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=newFolder & "\" & RFIPrefix & RFINum & JobNum & JobName & Exten, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=False
End Sub
--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Solution
Thanks Dante, I'll give it a try :)

Also, yes I mean create the folder in the folder where I have the Excel file with the macro
 
Upvote 0
Thanks Dante, that works fantastic!

That's exactly what I was after :)
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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