jujuarucih
New Member
- Joined
- Nov 14, 2023
- Messages
- 2
- Office Version
- 2016
- Platform
- Windows
The aim is for the code to generate the pdf, save it and attach it to an email in outlook.
The following error appears:
Runtime error '1004':
The document was not saved. It may be open or there may have been an error during saving
The following error appears:
Runtime error '1004':
The document was not saved. It may be open or there may have been an error during saving
VBA Code:
Private Sub CommandButton13_Click()
Dim ws As Worksheet
Dim rng As Range
Dim fileName As String
Set ws = ThisWorkbook.Sheets("PDF")
Set rng = Union(ws.Range("E2"), ws.Range("G9"), ws.Range("M9"))
fileName = "New Contract_" & ws.Range("G9").Value & "_" & ws.Range("M9").Value & "_" & ws.Range("E2").Value
ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
Environ("USERPROFILE") & "\Downloads\" & fileName, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Set outlookApp = CreateObject("Outlook.Application")
Set outlookMail = outlookApp.CreateItem(0)
With outlookMail
.Subject = fileName
.Body = "Dear Leslie. Please follow the attached contract! "
.To = "leslie.almeida@ui.com"
.Attachments.Add Environ("USERPROFILE") & "\Downloads\" & fileName & ".pdf"
.Display
End With
Set outlookMail = Nothing
Set outlookApp = Nothing