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)
 
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!
That's sorted it 🙂 Thank you so much! You're a star!
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,223,884
Messages
6,175,177
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