Setting the name of a PDF file export

Jimbob2000

New Member
Joined
Jun 27, 2019
Messages
25
Hey there!
I have some VBA code that I slightly adapted from the internet and it's working pretty well. The code takes a page on my workbook, exports it to a PDF, then attaches it to an email. I work for a non-profit and I'm hoping to use this to speed up invoicing for some of our training services.

The problem is that the code automatically names the PDF file the same as the worksheet, whereas I want it to name the PDF based on the value of a cell (say, C16). Right now the invoice pdf is always called "Invoice" and I'd just like it to be more descriptive and sound less dubious.

Here's my borrowed code, if you could suggest an edit, that would be amazing!

Code:
Sub Export_Invoice()



Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range
 
Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
 
If xFileDlg.Show = True Then
   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + ".pdf"
 
'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
        Kill xFolder
    Else
        MsgBox "if you don't overwrite the existing PDF, I can't continue." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
        Exit Sub
    End If
    If Err.Number <> 0 Then
        MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
        Exit Sub
    End If
End If
 
Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file
    xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
     
    'Create Outlook email
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .To = ActiveWorkbook.Sheets("Invoice Template").Range("C24")
        .CC = "me@me.com"
        .Subject = ActiveWorkbook.Sheets("Invoice Template").Range("C16")
        .Attachments.Add xFolder
        If DisplayEmail = False Then
            '.Send
        End If
    End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
The problem is that the code automatically names the PDF file the same as the worksheet, whereas I want it to name the PDF based on the value of a cell (say, C16). Right now the invoice pdf is always called "Invoice" and I'd just like it to be more descriptive and sound less dubious.

The key is in this line:

Code:
xFolder = xFolder + "\" + xSht.Name + ".pdf"

Change xSht.Name to what you want to call the PDF. If you want it to be whatever is in C16 (which I assume is on xSht?), then write it like this:

Code:
xFolder = xFolder + "\" + xSht.Range("C16").Text + ".pdf"

If you want to make sure it's always in ActiveWorkbook.Sheets("Invoice Template"), then change the code like this:

Code:
xFolder = xFolder + "\" + ActiveWorkbook.Sheets("Invoice Template").Range("C16").Text + ".pdf"

If you go with either route, know that you can get errors with invalid file names; your error check takes care of this in terms of not crashing and throwing up the debugger, but it won't give you the right reason if this is what causes an error. It is up to you whether that's a big-enough problem (it wouldn't be for most people... I don't think).
 
Upvote 0
This worked perfectly - thank you! I also really appreciate you explaining the change you suggested -- feel like I learned something!
 
Upvote 0
Hi Jon! I wondered if I could ask a follow-up question?

If I wanted to have the option of using a default file path to save the PDF or to open the file dialogue using a Yes/No toggle in a cell (say, N21), how would I write that in VBA? I tried adding an If/Then clause above this section, but couldn't work out how to code the file path.

Code:
If xFileDlg.Show = True Then   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Range("L22").Text + ".pdf"
 
Upvote 0
Hi Jon! I wondered if I could ask a follow-up question?

If I wanted to have the option of using a default file path to save the PDF or to open the file dialogue using a Yes/No toggle in a cell (say, N21), how would I write that in VBA? I tried adding an If/Then clause above this section, but couldn't work out how to code the file path.

Code:
If xFileDlg.Show = True Then   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Range("L22").Text + ".pdf"
 
Upvote 0
Dear All,

Can anyone of you team assist me in this?

I want to link the PDF file name to a specific cell contain Now() function to be a unique name
or
to overwrite the file and replace the old every save.

If Len(Dir(xFolder)) > 0 Then
xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
vbYesNo + vbQuestion, "File Exists")
On Error Resume Next
If xYesorNo = vbYes Then
Kill xFolder
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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