Save pdf to a specific folder

trevolly

Board Regular
Joined
Aug 22, 2021
Messages
124
Office Version
  1. 365
Platform
  1. Windows
Hi all

I'm working on a workbook called "1282 Final" that when a "submit" button is pressed the vba code copies data to another sheet in the workbook, creates a pdf from the active worksheet, names the pdf in a very specific format, attaches the pdf to an email and then saves the pdf to the same folder path as where the "1282 Final" workbook is.

I'm really happy with how it all works but I'd like for when the pdf exports it saves in to a specific folder which has the path of "X:\Airfield Operations\2022 Airfield Inspection and Data\CA1282 Bird Strikes\PDF" rather than just where the workbook is. I do need the file name to be as the coding names it.......for example "1282 Final_Serial 28 - 27-11-2022"

I've attached the part of the vba code that refers to the creation of the pdf.... could anyone please help?

Thank you

VBA Code:
Sheets("Birdstrike").Select

'email and pdf
Dim IsCreated As Boolean
  Dim I As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object
 
  ' Title area
  Title = Range("C6")
 
  ' Define PDF filename
  PdfFile = ActiveWorkbook.FullName
  I = InStrRev(PdfFile, ".")
  If I > 1 Then PdfFile = Left(PdfFile, I - 1)
  PdfFile = PdfFile & "_Serial" & " " & Title & " - " & Format(Now(), "dd-mm-yyyy") & ".pdf"
 
  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
 
  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0
 
  ' Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)
   
    ' E-mail address and content
    .Subject = "1282 Bird Strike Form - Serial" & " " & Title & " - " & ActiveSheet.Range("F6")
    .To = "fredbloggs@me.com" ' <-- Put email of the recipient here
    .Body = "Hi Trevor," & vbLf & vbLf _
          & "Please find attached a PDF of the 1282 for the recent Wildlife Strike Incident." & vbLf & vbLf _
          & "Regards," & vbLf _
          & Application.UserName & vbLf & vbLf
    .Attachments.Add PdfFile
   
    ' Try to send
    On Error Resume Next
    .Display
    Application.Visible = True
    If Err Then
      MsgBox "E-mail was not generated", vbExclamation
    End If
    On Error GoTo 0
   
  End With
 
  ' Quit Outlook if it was created by this code
  If IsCreated Then OutlApp.Quit
 
  ' Release the memory of object variable
  Set OutlApp = Nothing
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi trevolly,
try this code, I just inserted a variable corresponding to your path

VBA Code:
Sheets("Birdstrike").Select

'email and pdf
Dim IsCreated As Boolean
  Dim I As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object
  Dim MyPath As String
 
  ' Title area
  Title = Range("C6")
 
 ' Define path
 MyPath = "X:\Airfield Operations\2022 Airfield Inspection and Data\CA1282 Bird Strikes\PDF\"
 
  ' Define PDF filename
  PdfFile = ActiveWorkbook.Name 'File name without path
  I = InStrRev(PdfFile, ".")
  If I > 1 Then PdfFile = Left(PdfFile, I - 1)
  PdfFile = MyPath & PdfFile & "_Serial" & " " & Title & " - " & Format(Now(), "dd-mm-yyyy") & ".pdf"
 
Upvote 0
Solution
Hi all

Earlier this week I asked for help with exporting an excel sheet as a pdf to a specific folder.... I was helped and it worked fine but in the last couple of days the pdf doesn't save in to the specific path I've listed - only in to the same folder that the original workbook is in. I've listed the part of vba that relates to the creation of the pdf, attaching it to an email and then exporting it to the specified folder and wondered if anyone would be able to see any glaring errors? I need the PDF to save in to a folder as "CA1282 V2_Serial 5 - 02-12-22" for example (the serial no - retrieved from cell C6 on the worksheet, and date will change to the current date). Maybe I've accidently changed something that is tripping the vba up? I'd appreciate any help

Kind Regards


VBA Code:
Sheets("Birdstrike").Select

'email and pdf
Dim IsCreated As Boolean
  Dim I As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object
  Dim MyPath As String
 
  ' Title area
  Title = Range("C6")
 
 ' Define path
 MyPath = "X:\Airfield Operations\2022 Airfield Inspection and Data\CA1282 Wildlife Strikes\PDF"

 
  ' Define PDF filename
  PdfFile = ActiveWorkbook.Name 'File name without path
  I = InStrRev(PdfFile, ".")
  If I > 1 Then PdfFile = Left(PdfFile, I - 1)
  PdfFile = "CA1282" & "Serial" & " " & Title & " - " & Format(Now(), "dd-mm-yyyy") & ".pdf"
 
  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
  
 
  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0
 
  ' Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)
   
    ' E-mail address and content
    .Subject = "1282 Bird Strike Form - Serial" & " " & Title & " - " & ActiveSheet.Range("F6")
    '.To = "fredbloggs@me.com" ' <-- Put email of the recipient here
    '.CC =  'CC details
    .Body = "Hi Vicki," & vbLf & vbLf _
          & "Please find attached the main information, photos and a PDF of the CA1282 form for a recent Wildlife Strike Incident." & vbLf & vbLf _
          & "Kind Regards," & vbLf _
          & Application.UserName & vbLf & vbLf
    .Attachments.Add PdfFile
   
    ' Try to send
    On Error Resume Next
    .Display
    Application.Visible = True
    If Err Then
      MsgBox "E-mail was not generated", vbExclamation
    End If
    On Error GoTo 0
   
  End With
 
Upvote 0
Hi trevolly,
it's possible you forgot the final slash
VBA Code:
 MyPath = "X:\Airfield Operations\2022 Airfield Inspection and Data\CA1282 Bird Strikes\PDF\"
 
Upvote 0
Hey @Sequoyah thanks for replying. I’ve tried that and it didn’t help. The workbook is in X:\Airfield Operations\2022 Airfield Inspection and Data\CA1282 Wildlife Strikes (the folder below where I want the pdf to save to). I’ll keep looking!!
 
Upvote 0
Hi trevolly,
you also need to enter the path

VBA Code:
PdfFile =  MyPath & "CA1282" & "Serial" & " " & Title & " - " & Format(Now(), "dd-mm-yyyy") & ".pdf"
 
Upvote 0
That was it!!! Thank you so much @Sequoyah Adding that got it working. Thank you for your time and patience.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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