Hi All
New here and hoping for some advice please...
I have a macro created which automatically saves a pdf version of an excel sheet to a specified folder and then opens outlook to send.
I've managed to get it all working well, except the file name is pulling from the tab name rather than cell Q1 in the sheet I need it to. I've tried various different combinations but keep getting a syntax error further down so have completely hit the wall now
Offending lines are in bold
Any advice you could give would be fab! Worth noting I've never had much to do with VBA - this is my first go so apologies if I lack understanding!
My code is as per below:
Sub SaveAsPDFandSend()
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 xPath As String
Set xSht = ActiveSheet
xPath = "\\CUKNSFSIPS001\InsulationUK\Sales\05. Customer Pricing\02. Customer Support\01. Pending Support Applications\" 'here "workshet to pdf" is the destination folder to save the pdf files
xFolder = xPath + "\" + xSht.Name + ".pdf"
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)
'Create message
On Error Resume Next
With xEmailObj
.To = Range("Q3")
.CC = Range("Q4") & "; " & Range("Q5")
.Subject = Range("Q6")
.Body = "Hello " & Range("Q7") & vbNewLine & _
"" & vbNewLine & _
"Please see attached Support Contract " & Range("Q8") & " for " & Range("Q9") & ", " & Range("Q10") & "." & vbNewLine & _
"" & vbNewLine & _
"Kind Regards"
.Attachments.Add xFolder
.Display 'Use only during debugging ##############################
'.Send 'Uncomment to send e-mail ##############################
End With
Else
MsgBox "The active worksheet cannot be blank"
Exit Sub
End If
End Sub
Bookmarks