Name PDF by multiple cell range

Oldat43

New Member
Joined
Nov 16, 2021
Messages
12
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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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"
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,291
Members
448,564
Latest member
ED38

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