Skip Sending an Email If Status is Yes (already sent)

lharr28

New Member
Joined
May 22, 2024
Messages
24
Office Version
  1. 365
Platform
  1. Windows
I have VBA to send bulk emails from a word template using outlook. I'm trying to figure out how to get the file to skip sending an email if the status for sent is marked as yes. Below I've included a screenshot of the excel file and the code I have so far. I'm fairly new to VBA and I'm thinking some kind of if statement is needed.

1724201945985.png


Rich (BB code):
Sub sendMail()

Dim ol As Outlook.Application
Dim olm As Outlook.MailItem

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 Integer

For r = 11 To Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
    Set olm = ol.CreateItem(olMailItem)
    
    '***pulling the template to use, 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, 2).Value '****info is in column 2 or B
    .Execute Replace:=wdReplaceAll
    End With
    
    With wd.Selection.Find
    .Text = "<<Merchant>>"
    .Replacement.Text = Sheet4.Cells(r, 4).Value '****info is in column 4 or D
    .Execute Replace:=wdReplaceAll
    End With
    
    With wd.Selection.Find
    .Text = "<<Amount>>"
    .Replacement.Text = Sheet4.Cells(r, 5).Value '****info is in column 5 or E
    .Execute Replace:=wdReplaceAll
    End With
    
    With wd.Selection.Find
    .Text = "<<transaction date>>"
    .Replacement.Text = Sheet4.Cells(r, 3).Value '****info is in column 5 or E
    .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, 6).Value
        .Subject = Sheet4.Cells(r, 8).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

Next

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi Iharr28,

Try this:

VBA Code:
Option Explicit
Sub sendMail()

    '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 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
        If StrConv(Cells(r, "J"), vbProperCase) = "No" Then
            Set olm = ol.CreateItem(olMailItem)
            
            '***pulling the template to use, 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, 2).Value '****info is in column 2 or B
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<Merchant>>"
            .Replacement.Text = Sheet4.Cells(r, 4).Value '****info is in column 4 or D
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<Amount>>"
            .Replacement.Text = Sheet4.Cells(r, 5).Value '****info is in column 5 or E
            .Execute Replace:=wdReplaceAll
            End With
            
            With wd.Selection.Find
            .Text = "<<transaction date>>"
            .Replacement.Text = Sheet4.Cells(r, 3).Value '****info is in column 5 or E
            .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, 6).Value
                .Subject = Sheet4.Cells(r, 8).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
            
        End If
    
    Next r

End Sub

Regards,

Robert
 
Upvote 0
Hi Iharr28,

Try this:

VBA Code:
Option Explicit
Sub sendMail()

    '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 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
        If StrConv(Cells(r, "J"), vbProperCase) = "No" Then
            Set olm = ol.CreateItem(olMailItem)
           
            '***pulling the template to use, 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, 2).Value '****info is in column 2 or B
            .Execute Replace:=wdReplaceAll
            End With
           
            With wd.Selection.Find
            .Text = "<<Merchant>>"
            .Replacement.Text = Sheet4.Cells(r, 4).Value '****info is in column 4 or D
            .Execute Replace:=wdReplaceAll
            End With
           
            With wd.Selection.Find
            .Text = "<<Amount>>"
            .Replacement.Text = Sheet4.Cells(r, 5).Value '****info is in column 5 or E
            .Execute Replace:=wdReplaceAll
            End With
           
            With wd.Selection.Find
            .Text = "<<transaction date>>"
            .Replacement.Text = Sheet4.Cells(r, 3).Value '****info is in column 5 or E
            .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, 6).Value
                .Subject = Sheet4.Cells(r, 8).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
           
        End If
   
    Next r

End Sub

Regards,

Robert
Thanks Robert! It works perfectly! Would you happen to have a good reference that explains "If StrConv(Cells(r, "J"), vbProperCase)"?
 
Upvote 0
Thanks Robert! It works perfectly!

That's great and you're welcome.

Would you happen to have a good reference that explains "If StrConv(Cells(r, "J"), vbProperCase)"?

The code is checking the active row (r) in Col. J 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).
 
Upvote 1

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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