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,
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: