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!
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