SAVING A PDF SPREADSHEET IN EXCEL DOESN'T WORK

jujuarucih

New Member
Joined
Nov 14, 2023
Messages
2
Office Version
  1. 2016
Platform
  1. 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


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
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi,
untested but see if this update to your code resolves the issue

VBA Code:
Private Sub CommandButton13_Click()
    Dim ws          As Worksheet
    Dim rng         As Range
    Dim strFileName As String, strFolderName As String
    
    strFolderName = Environ("USERPROFILE") & "\Downloads\"
    
    On Error GoTo myerror
    Set ws = ThisWorkbook.Sheets("PDF")
    
    Set rng = Union(ws.Range("E2"), ws.Range("G9"), ws.Range("M9"))
    
    strFileName = "New Contract_" & rng.Areas(2).Value & "_" & _
                                    rng.Areas(3).Value & "_" & _
                                    rng.Areas(1).Value & ".pdf"

    ws.ExportAsFixedFormat Type:=xlTypePDF, FileName:=strFolderName & strFileName, Quality:= _
                           xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                           OpenAfterPublish:=False

    Set outlookApp = CreateObject("Outlook.Application")
    Set outlookMail = outlookApp.CreateItem(0)
    
    With outlookMail
        .Subject = Split(strFileName, ".")(0)
        .Body = "Dear Leslie. Please follow the attached contract! "
        .To = "leslie.almeida@ui.com"
        
        .Attachments.Add strFolderName & strFileName
        
        .Display
        
    End With
    
myerror:
    Set outlookMail = Nothing
    Set outlookApp = Nothing
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
    
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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