Run-time error 1004

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
779
Office Version
  1. 365
Getting this run-time error 1004 and when go debug highlight the the following line of code:


Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating

thank you,
 
When the error occurs, click on Debug. Then, move your cursor over the variable PDFFile. What does it display? Does it display a valid path and filename?
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
again:
exactly what do you have in the cells?
exactly what do you have in the variable PDFfile at the time of saving?

Check that you have closed the file to create.

Only for one test, change this:

Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating


For this:

Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=[COLOR=#0000ff]ThisWorkbook.Path & "\file.pdf"[/COLOR], _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=[COLOR=#0000ff]False[/COLOR]

Try and tell me.
 
Upvote 0
Hi,

i changed as suggested it opens outlook then give me this error:

Run-time error -2147024893 (80070003)
Path does not exixt. Verify the path is correct.

then press debug and highlight this line:

Code:
.Attachments.Add PDFFile

thank you for all your help.
 
Upvote 0
again:
exactly what do you have in the cells? H6, A3, A100
exactly what do you have in the variable PDFfile at the time of saving?


You have to provide the information you have on your sheet, without it we can not help, we are only assuming.

Code:
 CurrentMonth = Mid(ActiveSheet.Range("H6").Value, InStr(1, ActiveSheet.Range("A3").Value, " ") + 1)
    
    'Create new PDF file name including path and file extension
    PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Range("A100") _
               & "_" & CurrentMonth & ".pdf"
 
Last edited:
Upvote 0
Hi,

H6 holds month/year
A3 has Vendor account #
A100 has this formula: =TRIM("ACCOUNT RECONCILIATION - "&TEXT(STATEMENT!$A$1,)&"_"&A3)
 
Upvote 0
again:
exactly what do you have in the cells? H6, A3, A100
exactly what do you have in the variable PDFfile at the time of saving?

What data do you see on the screen, I do not want the formula, I want the data you see in the cell.
 
Upvote 0
A100
STATEMENT - Anixter_100013

H6 March-2019

A3: 1000013


<tbody>
</tbody>
 
Upvote 0
Here you have your updated code, try and tell me.


Code:
Sub create_and_email_4pdf() ' Author - Philip Treacy  ::   http://www.linkedin.com/in/philiptreacy
    ' http://www.MyOnlineTrainingHub.com/vba-to-create-pdf-from-excel-worksheet-then-email-it-with-outlook
    ' Date - 14 Oct 2013
    ' Create a PDF from the current sheet and email it as an attachment through Outlook
    
    
    Dim EmailSubject As String, EmailSignature As String
    Dim CurrentMonth As String, DestFolder As String, PDFFile As String
    Dim Email_To As String, Email_CC As String, Email_BCC As String, Email_body As String
    Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean
    Dim OverwritePDF As VbMsgBoxResult
    Dim OutlookApp As Object, OutlookMail As Object
    CurrentMonth = ""
    
    
    ' *****************************************************
    ' *****     You Can Change These Variables    *********




    EmailSubject = "NAVIGATA a/c " & " " & ActiveSheet.Range("A3") & "," & " " & "Statement Of Account $ " & Format(Range("I1").Value, "$#,##0.00;($#,##0.00)") 'Change this to change the subject of the email. The current month is added to end of subj line
    OpenPDFAfterCreating = False    'Change this if you want to open the PDF after creating it : TRUE or FALSE
    AlwaysOverwritePDF = False      'Change this if you always want to overwrite a PDF that already exists :TRUE or FALSE
    DisplayEmail = True 'Change this if you don't want to display the email before sending.  Note, you must have a TO email address specified for this to work
    Email_To = ActiveSheet.Range("B3")   'Change this if you want to specify To email e.g. ActiveSheet.Range("H1") to get email from cell H1
    Email_CC = "accounts.receivable@navigata.ca"
    Email_BCC = ""
    Email_body = "" & "Hi" & ActiveSheet.Range("A2") & "," & "" & "" & ActiveSheet.Range("W3") & " " & "" & "" & "If you have any question regarding the attached statement, please let me know." & "" & "" & "" & "" & "Regards,"
    ' ******************************************************
    
    'Prompt for file destination
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = True Then
            'DestFolder = "C:\Documents and Settings\rossj1\Desktop\Excel Files\REMITTANCE\EXCEL_PDF" '.SelectedItems(1)
            DestFolder = ThisWorkbook.Path
        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
    End With


    'Create new PDF file name including path and file extension
    CurrentMonth = Format(ActiveSheet.Range("H6").Value, "mm-dd-yyyy")
    PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Range("A100") & "_" & CurrentMonth & ".pdf"


    'If the PDF already exists
    If Len(Dir(PDFFile)) > 0 Then
        If AlwaysOverwritePDF = False Then
            OverwritePDF = MsgBox(PDFFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "File Exists")
            On Error Resume Next
            'If you want to overwrite the file then delete the current one
            If OverwritePDF = vbYes Then
                Kill PDFFile
            Else
                MsgBox "OK then, 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
        Else
            On Error Resume Next
            Kill PDFFile
        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


    'Create the PDF
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=OpenPDFAfterCreating


    'Create an Outlook object and new mail message
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
        
    'Display email and specify To, Subject, etc
    With OutlookMail
        .display
        .To = Email_To
        .CC = Email_CC
        .BCC = Email_BCC
        .Subject = EmailSubject & CurrentMonth
        '.Body = Email_body
        .HTMLBody = Email_body & "" & .HTMLBody
        .Attachments.Add PDFFile
        If DisplayEmail = False Then
            .Send
        End If
    End With
End Sub
 
Upvote 0
Thank you, worked good.

just one question how can I make to greetings two spaces down for the body message as now shows this, if you notice after Hi0 shows zero or o don't know why this exactly shows:

Hi0,Attached is the Reconciliation of the account, as at today the amount owing is $51.46 please confirm if agree with your records. Thank you.. If you have any question regarding the attached reconciliation. please let me know.Regards,.

Also the subject line how give a comma in the amount space and then the date, right now is showing all together as below,

NAVIGATA a/c 100013, Statement Of Account $ $51.4603-22-2019
 
Upvote 0
Use this
Note: the zero appears because that is what you have in cell A2

Code:
Sub create_and_email_4pdf() ' Author - Philip Treacy  ::   http://www.linkedin.com/in/philiptreacy
    ' http://www.MyOnlineTrainingHub.com/vba-to-create-pdf-from-excel-worksheet-then-email-it-with-outlook
    ' Date - 14 Oct 2013
    ' Create a PDF from the current sheet and email it as an attachment through Outlook
    
    
    Dim EmailSubject As String, EmailSignature As String
    Dim CurrentMonth As String, DestFolder As String, PDFFile As String
    Dim Email_To As String, Email_CC As String, Email_BCC As String, Email_body As String
    Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean
    Dim OverwritePDF As VbMsgBoxResult
    Dim OutlookApp As Object, OutlookMail As Object
    CurrentMonth = ""
    
    
    ' *****************************************************
    ' *****     You Can Change These Variables    *********




    EmailSubject = "NAVIGATA a/c " & " " & ActiveSheet.Range("A3") & ",  Statement Of Account $ " & Format(Range("I1").Value, "$#,##0.00;($#,##0.00)") 'Change this to change the subject of the email. The current month is added to end of subj line
    OpenPDFAfterCreating = False    'Change this if you want to open the PDF after creating it : TRUE or FALSE
    AlwaysOverwritePDF = False      'Change this if you always want to overwrite a PDF that already exists :TRUE or FALSE
    DisplayEmail = True 'Change this if you don't want to display the email before sending.  Note, you must have a TO email address specified for this to work
    Email_To = ActiveSheet.Range("B3")   'Change this if you want to specify To email e.g. ActiveSheet.Range("H1") to get email from cell H1
    Email_CC = "accounts.receivable@navigata.ca"
    Email_BCC = ""
    Email_body = "Hi " & ActiveSheet.Range("A2") & ", " & ActiveSheet.Range("W3") & " " & _
                 "If you have any question regarding the attached statement, please let me know. <br> <br> " & _
                 "Regards."
    ' ******************************************************
    
    'Prompt for file destination
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = True Then
            'DestFolder = "C:\Documents and Settings\rossj1\Desktop\Excel Files\REMITTANCE\EXCEL_PDF" '.SelectedItems(1)
            DestFolder = ThisWorkbook.Path
        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
    End With


    'Create new PDF file name including path and file extension
    CurrentMonth = Format(ActiveSheet.Range("H6").Value, "mm-dd-yyyy")
    PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Range("A100") & "_" & CurrentMonth & ".pdf"


    'If the PDF already exists
    If Len(Dir(PDFFile)) > 0 Then
        If AlwaysOverwritePDF = False Then
            OverwritePDF = MsgBox(PDFFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "File Exists")
            On Error Resume Next
            'If you want to overwrite the file then delete the current one
            If OverwritePDF = vbYes Then
                Kill PDFFile
            Else
                MsgBox "OK then, 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
        Else
            On Error Resume Next
            Kill PDFFile
        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


    'Create the PDF
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=OpenPDFAfterCreating


    'Create an Outlook object and new mail message
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
        
    'Display email and specify To, Subject, etc
    With OutlookMail
        .display
        .To = Email_To
        .CC = Email_CC
        .BCC = Email_BCC
        .Subject = EmailSubject & ", " & CurrentMonth
        '.Body = Email_body
        .HTMLBody = Email_body & "" & .HTMLBody
        .Attachments.Add PDFFile
        If DisplayEmail = False Then
            '.Send
        End If
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,605
Messages
6,120,476
Members
448,967
Latest member
visheshkotha

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