Using an if statement to add an attachment to an email

Tablecloth98

New Member
Joined
Nov 15, 2023
Messages
24
Office Version
  1. 2021
Platform
  1. Windows
I need to use an if statement to add an attachment to an email if cell G5 contains specific text (e.g. "Specific Text").

I've tried using
If Range("G5").Value = "Specific Text" Then
.Attachment.Add "C:\Documents\MadeUpFileLocation\ImageName.JPG"
End If

but the debug is highlighting the attachment line.

Am I doing something wrong? (Apologies if this is a silly question. I'm very new to vba)
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Check that the folder name is correct


Check that the file name is correct


Check that the file exists in the folder.


:cool:
Sorry about the names listed 😂 it's sensitive information so I had to come up with some dummy data.

I'm wondering if I'm putting the if statement in the wrong place. I was putting it after the email body as I'd planned to embed the images after the body text
 
Upvote 0
You are missing the letter "S":

.AttachmentS.Add "C:\Documents\MadeUpFileLocation\ImageName.JPG"
 
Upvote 0
You are missing the letter "S":

.AttachmentS.Add "C:\Documents\MadeUpFileLocation\ImageName.JPG"
It's highlighting the word attachments and saying 'Compile error: Invalid or unqualified reference' for some reason 😂 I'm really bad at this
 
Upvote 0
Try...

VBA Code:
.Attachments.Add "C:\Users\Documents\MadeUpFileLocation\ImageName.JPG"

Hope this helps!
 
Upvote 0
'Compile error: Invalid or unqualified reference'
You should put all your code here to see the objects you are using, maybe you are not using the right objects.

Or check the following example:

VBA Code:
Sub exampleemail()
  Dim OutApp As Object, OutMail As Object

  Set OutApp = CreateObject("Outlook.Application")
  Set OutMail = OutApp.CreateItem(0)
  
  With OutMail
    .To = "email@domain.com"
    .Subject = "This is the Subject line"
    If Range("G5").Value = "Specific Text" Then
      .Attachments.Add "C:\trabajo\files\report.xlsx"
    End If
    '.Send
    .Display
  End With
End Sub

:cool:
 
Upvote 0
You should put all your code here to see the objects you are using, maybe you are not using the right objects.

Or check the following example:

VBA Code:
Sub exampleemail()
  Dim OutApp As Object, OutMail As Object

  Set OutApp = CreateObject("Outlook.Application")
  Set OutMail = OutApp.CreateItem(0)
 
  With OutMail
    .To = "email@domain.com"
    .Subject = "This is the Subject line"
    If Range("G5").Value = "Specific Text" Then
      .Attachments.Add "C:\trabajo\files\report.xlsx"
    End If
    '.Send
    .Display
  End With
End Sub

:cool:
Try...

VBA Code:
.Attachments.Add "C:\Users\Documents\MadeUpFileLocation\ImageName.JPG"

Hope this helps!
Still can't seem to get it working. My full code is here (sensitive info omitted but all file names and locations have been double checked):

Sub Process1()



Dim App As Object, Itm As Object

Dim EmailSubject As String

Dim SendTo As String

Dim EmailBody As String

Dim ccTo As String

Dim i As Long



Application.ScreenUpdating = False

Application.DisplayAlerts = False



Range("B2") = Date

Range("B3") = Time



For i = 5 To Range("B" & Rows.Count).End(3).Row

If Not IsEmpty(Range("B" & i).Value) Then

Set App = CreateObject("Outlook.Application")

Set Itm = App.createitem(0)



SendTo = Range("B" & i).Value

EmailSubject = Account Number- " & Range("C" & i).Value

EmailBody = Range("I3") & vbNewLine & vbNewLine & Range("I" & i).Value & Range("M" & i) & Range("H" & i).Value & Range("J" & i).Value & vbNewLine & Range("B1") & vbNewLine & Range("D1") & vbNewLine & Range("K" & i).Value

If Range("G" & i).Value = "1" Then

.Attachment.Add "C:\Users\UsernameOmitted\Documents\Image1.JPG"
.HTMLBody = "<img src=""cid:Image1.jpg"

End If



With Itm

.SentOnBehalfOfName = Range("L" & i).Value

.Subject = EmailSubject

.To = SendTo

.Body = EmailBody



.Send

End With



Set App = Nothing

Set Itm = Nothing

End If

Next



Application.ScreenUpdating = True

Application.DisplayAlerts = True



MsgBox "Process Complete", vbInformation



End Sub

Thank you for all of your help 🙂
 
Upvote 0
Try it like this...

VBA Code:
Option Explicit

Sub Process1()

    Dim App As Object, Itm As Object
    Dim EmailSubject As String
    Dim SendTo As String
    Dim EmailBody As String
    Dim ccTo As String
    Dim i As Long
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    Range("B2").Value = Date
    Range("B3").Value = Time
   
    Set App = CreateObject("Outlook.Application")
   
    For i = 5 To Range("B" & Rows.Count).End(3).Row
        If Not IsEmpty(Range("B" & i).Value) Then
            SendTo = Range("B" & i).Value
            EmailSubject = "Account Number- " & Range("C" & i).Value
            EmailBody = Range("I3").Value & vbNewLine & vbNewLine & Range("I" & i).Value & Range("M" & i).Value & Range("H" & i).Value & Range("J" & i).Value & vbNewLine & Range("B1").Value & vbNewLine & Range("D1").Value & vbNewLine & Range("K" & i).Value
            Set Itm = App.createitem(0)
            With Itm
                .SentOnBehalfOfName = Range("L" & i).Value
                .Subject = EmailSubject
                .To = SendTo
                .Body = EmailBody
                If Range("G" & i).Value = "1" Then
                    .Attachments.Add "C:\Users\UsernameOmitted\Documents\Image1.JPG"
                    .HTMLBody = .HTMLBody & "<img src=""cid:Image1.jpg"">"
                End If
                .Send
            End With
            Set Itm = Nothing
        End If
    Next i
   
    Set App = Nothing
   
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
   
    MsgBox "Process Complete", vbInformation

End Sub

Note that if you want the image placed at the beginning of the email instead of the end, replace...

VBA Code:
.HTMLBody = .HTMLBody & "<img src=""cid:Image1.jpg"">"

with

VBA Code:
.HTMLBody = "<img src=""cid:Image1.jpg"">" & .HTMLBody

Hope this helps!
 
Last edited:
Upvote 1
Solution

Forum statistics

Threads
1,223,879
Messages
6,175,147
Members
452,615
Latest member
bogeys2birdies

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