Excel VBA Loop Though Table for e-Mails

Hello,

I have a two-column table that contains "Name" and "e-Mail".

How would I edit the below VBA Script to loop through that table and generate an e-Mail for each e-Mail in the Table, and put the Name as the strbody variable?

Sub Mail_Outlook_With_Signature_HTML()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim SigString As String
    Dim Signature As String
    Dim SigVariable As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    SigVariable = Range("D2").Value
    SubString = Range("E2").Value

    strbody = ""

    'Change only Mysig.htm to the name of your signature
    SigString = Environ("appdata") & _
                "\Microsoft\Signatures\" & SigVariable & ".htm"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If

    On Error Resume Next

    With OutMail
        .Display
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = SubString
        .HTMLBody = strbody & "<br>" & Signature
        .Display
    End With

    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

Open in new window

EnigmaMatterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gplanaCommented:
Here is an explanation in how to loop through a range of cells from VBA: http://www.excel-easy.com/vba/examples/loop-through-defined-range.html

So I would try something like this:

Sub Mail_Outlook_With_Signature_HTML()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim SigString As String
    Dim Signature As String
    Dim SigVariable As String
    Dim rng As Range, cell As Range 'Define range variables

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    SigVariable = Range("D2").Value
    SubString = Range("E2").Value
    Set rng = Range("A1:A30") 'This is the range that contains your e-mail address

    strbody = ""

    'Change only Mysig.htm to the name of your signature
    SigString = Environ("appdata") & _
                "\Microsoft\Signatures\" & SigVariable & ".htm"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If

    On Error Resume Next

For Each cell In rng 'This is the loop


    With OutMail
        .Display
        .To = cell.value()
        .CC = ""
        .BCC = ""
        .Subject = SubString
        .HTMLBody = strbody & "<br>" & Signature
        .Display
    End With
Next cell

    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.