Name PDF by multiple cell range

Oldat43

New Member
Joined
Nov 16, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all, Not sure if this is already on the boards somewhere and apologies if it is.

I'm very new to macro coding, but I have an invoice template that prints, saves as a pdf and emails, it saves the name as the current date.
My problem is if I send more than one it wants to over write the last invoice of that name and I need to keep them. Can someone edit and add so it saves the cell range and adds the current date as well?

For example, now it saves (Invoice-2021-11-16-10-20-02 .pdf) I would like it to save (Invoice-2021-11-16-Business-name .pdf) The business name would be in cell B11.

Below is the macro im using, it works like a dream other than the name it saves with.

Like I said I'm a complete noob and this code is not mine, I was given it in the invoice, Thank you in advance,

VBA Code:
Sub BuddySaveFinal()

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
Dim xStr As String

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
xStr = Format(Now(), "yyyy-mm-dd-hh-mm-ss ")
xFolder = xFolder + "\" + xSht.Name + "-" + xStr + ".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 = Range("B16")
'.CC = Range("")
'.BCC = Range("")
.Subject = xSht.Name + "-" + xStr + ".pdf"
.Body = "Hi " _
& vbNewLine & vbNewLine & _
" " & _
""
.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
 
Last edited by a moderator:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

rlv01

Well-known Member
Joined
May 16, 2017
Messages
1,415
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
For example, now it saves (Invoice-2021-11-16-10-20-02 .pdf) I would like it to save (Invoice-2021-11-16-Business-name .pdf) The business name would be in cell B11.

Something like this perhaps.
VBA Code:
xStr = Format(Now(), "yyyy-mm-dd ") & xSht.Range("B11")
xFolder = xFolder & "\" & xSht.Name & "-" & xStr & ".pdf"
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,251
Messages
5,836,266
Members
430,414
Latest member
ayla

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
Top