Compile Error when Adding Attachments

lharr28

New Member
Joined
May 22, 2024
Messages
25
Office Version
  1. 365
Platform
  1. Windows
I'm trying to get the code to include an attachment if the file's path is entered in column G. When I run the macros I get an compile error message. I tried to define the attachment but it didn't work. I'm thinking something is wrong with the if/then syntax I added. Below I've included a copy of the error message, I screenshot of the document, and the VBA Code.

1724257177240.png


1724257325286.png


VBA Code:
Sub sendMail()
'Add microsoft word, outlook library object tools
'As the code uses early binding it needs references to:
    'Microsoft Outlook nn.n Object Library
    'Microsoft Word nn.n Object Library

    Dim ol As Outlook.Application
    Dim olm As Outlook.MailItem
    Dim Attachments As Outlook.Attachments
    
    Dim wd As Word.Application
    Dim doc As Word.Document
    
    Set ol = New Outlook.Application
    
'***Start from row 11 and go to the last row with data
    Dim r As Long
    
    For r = 11 To Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
        '***will send email if sent status is "No"
        If StrConv(Cells(r, "K"), vbProperCase) = "No" Then '***the code is checking the active row (r) in Col. K and if the text is "No" (I use proper case so there's no question on matching the text) it runs the script else it goes to the next row (r)
            If Sheet4.Cells(r, 7).Value <> "" Then
            Set olm = ol.CreateItem(olMailItem)
            
        '***pulling the template to use, note: document must be saved as a word template!
            Set wd = New Word.Application
            Set doc = wd.documents.Open(Cells(6, 2).Value)
        
            With wd.Selection.Find
            .Text = "<<first name>>"
            .Replacement.Text = Sheet4.Cells(r, 3).Value '****info is in column 3 or C
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<Merchant>>"
            .Replacement.Text = Sheet4.Cells(r, 9).Value '****info is in column 9 or I
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<Amount>>"
            .Replacement.Text = Sheet4.Cells(r, 10).Value '****info is in column 10 or J
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<transaction date>>"
            .Replacement.Text = Sheet4.Cells(r, 8).Value '****info is in column 8 or H
            .Execute Replace:=wdReplaceAll
            End With
        
            doc.Content.Copy
            
        '***Set the properties of the mail item, to, cc, subject, etc...
            With olm
                .Display
                .To = Sheet4.Cells(r, 4).Value
                .CC = Sheet4.Cells(r, 5).Value
                .Subject = Sheet4.Cells(r, 6).Value
            
        '***Copying the information from the word document into the body of the email
                Dim editor As Object
                Set editor = .GetInspector.WordEditor
                editor.Content.Paste
                '.Send
            End With
        
            Set olm = Nothing
        
            Application.DisplayAlerts = False
            doc.Close SaveChanges:=False
            Set doc = Nothing
            wd.Quit
            Set wd = Nothing
            Application.DisplayAlerts = True
            
            .Attachments.Add Sheet4.Cells(r, 7).Value
            End If
        
        End If
    
    Next r

End Sub
 

Attachments

  • 1724257144664.png
    1724257144664.png
    17.1 KB · Views: 3

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Your line of code
VBA Code:
            .Attachments.Add Sheet4.Cells(r, 7).Value
is written to be inside the With statement, but it's not. Move the End With down below this line, or in this particular case you can move this line up above the End With.
 
Upvote 0
Thanks Jeff! I tried this and it did work but now it only works if I have an attachment listed in the attachment field (column G). How would I get it to send even if there is no attachment listed in that filed. I've included the code below of what I did.

VBA Code:
For r = 11 To Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
        '***will send email if sent status is "No"
        If StrConv(Cells(r, "K"), vbProperCase) = "No" Then 
            If Sheet4.Cells(r, 7).Value <> "" Then
            Set olm = ol.CreateItem(olMailItem)
            
        '***pulling the template to use, note: document must be saved as a word template!
            Set wd = New Word.Application
            Set doc = wd.documents.Open(Cells(6, 2).Value)
        
            With wd.Selection.Find
            .Text = "<<first name>>"
            .Replacement.Text = Sheet4.Cells(r, 3).Value '****info is in column 3 or C
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<Merchant>>"
            .Replacement.Text = Sheet4.Cells(r, 9).Value '****info is in column 9 or I
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<Amount>>"
            .Replacement.Text = Sheet4.Cells(r, 10).Value '****info is in column 10 or J
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<transaction date>>"
            .Replacement.Text = Sheet4.Cells(r, 8).Value '****info is in column 8 or H
            .Execute Replace:=wdReplaceAll
            End With
        
            doc.Content.Copy
            
        '***Set the properties of the mail item, to, cc, subject, etc...
            With olm
                .Display
                .To = Sheet4.Cells(r, 4).Value
                .CC = Sheet4.Cells(r, 5).Value
                .Subject = Sheet4.Cells(r, 6).Value
            
        '***Copying the information from the word document into the body of the email
                Dim editor As Object
                Set editor = .GetInspector.WordEditor
                editor.Content.Paste
                '.Send
            
            '***add attachment if field has data
            .Attachments.Add Sheet4.Cells(r, 7).Value
            End With
        
            Set olm = Nothing
        
            Application.DisplayAlerts = False
            doc.Close SaveChanges:=False
            Set doc = Nothing
            wd.Quit
            Set wd = Nothing
            Application.DisplayAlerts = True
            
            End If
            
            Next r
        
        End If
    
    Next r
 
Upvote 0
VBA Code:
            '***add attachment if field has data
            If Sheet4.Cells(r, 7).Value <> "" Then
               .Attachments.Add Sheet4.Cells(r, 7).Value
            End If

This does not test if the cell has a valid file path, just that it has something.
 
Upvote 0
VBA Code:
            '***add attachment if field has data
            If Sheet4.Cells(r, 7).Value <> "" Then
               .Attachments.Add Sheet4.Cells(r, 7).Value
            End If

This does not test if the cell has a valid file path, just that it has something.
Ok, I may have misinterpreted the meaning. I thought it meant that if the field isn't blank then it will attach that file. I will see if I can find a work around for this. Thanks, again :)
 
Upvote 0
I don't understand your latest comment.

I assumed that you were getting an error trying to attach something using a blank path, so the mail was not being sent.

I showed a code modification that will attach a file based on the pathname in column G of that row, but only if that cell is not blank. I wrapped it in that If statement, which was not present in your code.

However, that does not guarantee that the content of the cell is a valid pathname.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,080
Members
453,021
Latest member
Justyna P

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