Excel VBA for Outlook

ComputerNewbie1992

New Member
Joined
Jul 26, 2018
Messages
16
Hi All,

I have a spreadsheet which works well but my leadership have asked to expand it's capabilities (If it's possible). Unfortunately I'm useless at VBA coding and had to seek help on the code I'm currently using.
  • At the moment I have a text box which is transfered into the email body, but instead I've been asked to include a table within the email instead.
  • They've also asked if we can add attachments to the emails which can be changed for each product
    • (I've seen some codes which refer to a local drive, ideally we'd be able to add the attachments to the excel sheet somehow but if this is not possible then I'll have to use the local drive route)
In columns L+M I've tried to show the fields we'd need and tried to explain how I'd ideally like it to show ?‍♂️

I've posted the VBA code I'm using and a Mini sheet of the spreadsheet itself.

Many thanks in advance.


VBA Code:
Sub CreateEmails()
 
 Application.ScreenUpdating = False
 
 Dim OutApp As Object, OutMail As Object
 Dim rng As Range, fnd As Range
 Dim x As Long
 Dim Signature As String
 
 Set OutApp = CreateObject("Outlook.Application")
 
 For Each rng In Range("C10", Range("C" & Rows.Count).End(xlUp))
   
     If rng = "x" Then
 
     Set fnd = Range("J:J").Find(rng.Offset(, -1).Value, LookIn:=xlValues, lookat:=xlWhole)
         If Not fnd Is Nothing Then
 
         x = fnd.CurrentRegion.Offset(1).Cells.Count - 1
 
         Set OutMail = OutApp.CreateItem(0)
             With OutMail
               .display
               Signature = OutMail.HTMLbody
               .To = Join(Application.WorksheetFunction.Transpose(Range("J" & fnd.Row + 1).Resize(x).Value), ";")
              .cc = "Boss@outlook.co.uk"
               .Subject = Range("C2").Value
               .HTMLbody = "Hi " & rng.Offset(, 3) & "," & "<br><br>" & Range("C4") & "<br>"
               .HTMLbody = .HTMLbody & Signature
               .display
             End With
         End If
     End If
 
 Next rng
 
 Application.ScreenUpdating = True
 End Sub


Example.xlsm
ABCDEFGHIJKLM
1
2Email Subject:New Product
3
4Email Body:Please see below details of our latest product:
5
6
7
8Whole Market?Full Market
9
10Company AxPoCDavidCompany A
11Company BxPoCMarkCompanyA1@outlook.com
12Company CxPoCSteveCompanyA2@outlook.comMarketing MaterialAttaches to email
13Company DxPoCSophieCompanyA3@outlook.com
14Company ExPoCJessiaCompanyA4@outlook.comProduct Name
15Company FxPoCDavidCompanyA5@outlook.comProduct Features
16Company GxPoCMarkCompanyA6@outlook.comPrice
17Company HxPoCSteveOffer Valid to
18Company IxPoCSophieCompany B
19Company JxPoCJessiaCompanyB1@outlook.comHow I'd like the email to look:
20Company KxPoCDavidCompanyB2@outlook.com
21Company LxPoCMarkCompanyB3@outlook.comTo:(Company contacts in Column J)
22Company MxPoCSteveCompanyB4@outlook.comCc:Boss@outlook.co.uk
23Company NxPoCSophieCompanyB5@outlook.comSubject:(Cell C2)
24Company OxPoCJessiaCompanyB6@outlook.comEmail Body:
25Company PxPoCDavid
26Company QxPoCMarkCompany CHi [PoC]
27Company RxPoCSteveCompanyC1@outlook.comPlease see below details of our latest product:
28Company SxPoCSophieCompanyC2@outlook.com
29Company TxPoCJessiaCompanyC3@outlook.comProduct Name
30Company UxPoCDavidCompanyC4@outlook.comProduct Features
31Company VxPoCMarkCompanyC5@outlook.comPrice
32Company WxPoCSteveCompanyC6@outlook.comOffer Valid to
33Company XxPoCSophie
34Company YxPoCJessiaCompany D[Email Signature]
35Company ZxPoCDavidCompanyD1@outlook.com
Email list
Cell Formulas
RangeFormula
J10J10=B10
J18J18=B11
J26J26=B12
J34J34=B13
C10:C35C10=IF($C$8="Full Market","x","")
Cells with Data Validation
CellAllowCriteria
C8:E8ListFull Market, Blank
C10:C35Listx,
 
Last edited by a moderator:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
This will get started on the body portion. We can figure out the attachments later. Is it close so far? You can change the size of the whole table and the individual columns by changing the 300px and 150px numbers to suit your design.
VBA Code:
Sub CreateEmails()
    Application.ScreenUpdating = False
    
    Dim OutApp As Object, OutMail As Object
    Dim rng As Range, fnd As Range
    Dim x As Long
    Dim Signature As String
    
    Set OutApp = CreateObject("Outlook.Application")
    For Each rng In Range("C10", Range("C" & Rows.Count).End(xlUp))
        If rng = "x" Then
            Set fnd = Range("J:J").Find(rng.Offset(, -1).Value, LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                x = fnd.CurrentRegion.Offset(1).Cells.Count - 1
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    Signature = OutMail.HTMLBody
                    .To = Join(Application.WorksheetFunction.Transpose(Range("J" & fnd.Row + 1).Resize(x).Value), ";")
                    .CC = "Boss@outlook.co.uk"
                    .Subject = Range("C2").Value
                    .HTMLBody = "<html><head><style>table, th, td {  border: 1px solid black;border-collapse: collapse;}</style></head><body>"
                    .HTMLBody = .HTMLBody & "Hi " & rng.Offset(, 3) & ",<br><br>"
                    .HTMLBody = .HTMLBody & Range("C4") & "<br><br>"
                    .HTMLBody = .HTMLBody & "<table style=""width:300px"">"
                    .HTMLBody = .HTMLBody & "<tr><td style=""width:150px"">Product Name</td><td style=""width:150px"">" & Range("M14") & "</td></tr>"
                    .HTMLBody = .HTMLBody & "<tr><td>Product Features</td><td>" & Range("M15") & "</td></tr>"
                    .HTMLBody = .HTMLBody & "<tr><td>Price</td><td>" & Range("M16") & "</td></tr>"
                    .HTMLBody = .HTMLBody & "<tr><td>Offer Valid to</td><td>" & Range("M17") & "</td></tr>"
                    .HTMLBody = .HTMLBody & "</table>" '</body></html>"
                    .HTMLBody = .HTMLBody & Signature
                    .Display
                End With
            End If
        End If
    Next rng
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I'll add that, with spacing, the HTMLBody coding is designed to produce the following. Since whitespace is ignored, the html coding is minimized in the Outlook VBA code above. The Signature copied at the beginning of setting the HTMLBody includes the closing </body></html> tags, so it's merely commented in.
Code:
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;}
</style>
</head>
<body>
Hi "rng.Offset(, 3)",<br>
<br>
"Range("C4")"
<br>
<br>
<table style="width:300px">
    <tr>
        <td style="width:150px">Product Name</td>
        <td style="width:150px">"Range("M14")"</td>
    </tr>
    <tr>
        <td>Product Features</td>
        <td>"Range("M15")"</td>
    </tr>
    <tr>
        <td>Price</td>
        <td>"Range("M16")"</td>
    </tr>
    <tr>
        <td>Offer Valid to</td>
        <td>"Range("M17")"</td>
    </tr>
</table>
Signature
Helpful HTML code tags are available lots of places on the web. One example:
 
Upvote 0
No. I had tested only the message generation part in Outlook VBA with hard-coded data in place of the Excel range references. When I run the code with your data above now, there is an issue in that Company D only has one recipient. That fails the Transpose part. This code checks to see if there is only one recipient. Other than that, this code generates 4 emails for companies A-D just fine.
VBA Code:
Sub CreateEmails()
    Application.ScreenUpdating = False
    
    Dim OutApp As Object, OutMail As Object
    Dim rng As Range, fnd As Range
    Dim x As Long
    Dim Signature As String
    
    Set OutApp = CreateObject("Outlook.Application")
    For Each rng In Range("C10", Range("C" & Rows.Count).End(xlUp))
        If rng = "x" Then
            Set fnd = Range("J:J").Find(rng.Offset(, -1).Value, LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                x = fnd.CurrentRegion.Offset(1).Cells.Count - 1
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    Signature = OutMail.HTMLBody
                    If x > 1 Then
                        .To = Join(Application.WorksheetFunction.Transpose(Range("J" & fnd.Row + 1).Resize(x).Value), ";")
                    Else
                        .To = Range("J" & fnd.Row + 1).Value
                    End If
                    .CC = "Boss@outlook.co.uk"
                    .Subject = Range("C2").Value
                    .HTMLBody = "<html><head><style>table, th, td {  border: 1px solid black;border-collapse: collapse;}</style></head><body>"
                    .HTMLBody = .HTMLBody & "Hi " & rng.Offset(, 3) & ",<br><br>"
                    .HTMLBody = .HTMLBody & Range("C4") & "<br><br>"
                    .HTMLBody = .HTMLBody & "<table style=""width:300px"">"
                    .HTMLBody = .HTMLBody & "<tr><td style=""width:150px"">Product Name</td><td style=""width:150px"">" & Range("M14") & "</td></tr>"
                    .HTMLBody = .HTMLBody & "<tr><td>Product Features</td><td>" & Range("M15") & "</td></tr>"
                    .HTMLBody = .HTMLBody & "<tr><td>Price</td><td>" & Range("M16") & "</td></tr>"
                    .HTMLBody = .HTMLBody & "<tr><td>Offer Valid to</td><td>" & Range("M17") & "</td></tr>"
                    .HTMLBody = .HTMLBody & "</table>" '</body></html>"
                    .HTMLBody = .HTMLBody & Signature
                    .Display
                End With
            End If
        End If
    Next rng
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Do I need a particular version of outlook/microsoft excel for your code?

I'm using your code and no emails are populating. Are you able to send me a working excel file so I can look for the differences?
 
Upvote 0
I can share it a little later (gotta head out). For now, can you go through the code line-by-line in Debug mode and figure out what is going on?
 
Upvote 0
Thanks @shknbk2, the code works on your spreadsheet - I'll just use this as my template to save faffing about.

Is it possible to add attachments to the email? Ideally it would be something you can upload to the excel but not sure if this is possible.
 
Upvote 0
I haven't found any easy way to use VBA to extract an embedded file. A lot of what I've seen writes the embedded file to the disk. It would be easier to handle the attachments from a saved folder. Once the path and files are known, adding attachments is easy. You can put the file locations in cells and get them from the cell values.
 
Upvote 0

Forum statistics

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