Excel VBA Macro to send one email to a recipient listed multiple times along with excel table snapshot and attachments

AndreMateus

New Member
Joined
Mar 31, 2023
Messages
11
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Currently, I have a macro that sends a snapshot of a filtered table and pasts it in the body of an outlook email as text and sends it off to the email address listed in a certain cell along with a message in the body of the email. The outlook mailbox defaults to the primary mailbox and I need to be able to select which mailbox to send the email with.

Now, my spreadsheet contains multiple recipients and, often times a recipient may be listed more than once. For this reason, I would like to have only 1 email sent to the recipient even if their email shows multiple times on the table. Along with the snapshot of the table, I would like to add attachments. The number of attachments being sent to the recipient will vary depending on the number of times our recipient shows on the list.

The goal is to distribute soft token files to each requestor but each email must contain the appropriate information for each of our requestors.

Here's a brief summary of what I am looking for:
1 - Need to be able to choose with mailbox the email will be sent from
2 - Email must be sent to 1 recipient even if the same email is showing multiple times (see "Requestor" field).
3 - Email subject line is always the same one
4 - Email Body message is always the same expect it captures/filters the info that should only be sent to our recipient
5 - Must attach one or more files (depending on the number of files being sent to the same submitter). Only 1 file per row. (see "Soft token file name" and "requestor" field).
6 - Not sure if it is possible to have the script loop through the table and complete the above steps but automatically. If not, I would not mind filtering the requests manually and having the emails sent automatically.

Please see pictures below for more information on how my current table looks and how I would like the final email(s) to look like.


Current script:

Sub Soft_Token_Distribution()
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim path As String
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)


With newEmail

.To = Sheet4.Range("L2").Text
.CC = ""
.BCC = ""
.Subject = "SOW Contingent Worker - Remote Access Request"
.Body = "Hi, As part of your Create, Modify or Terminate SOW Contingent Worker ServiceNow request for the below user, you requested Remote Access for the user. Vendor Remote Access has been provisioned for this user. Please share the attached documents with the user so that they can configure their Vendor Remote Access.

regards,
My Signature"


.display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Sheet4.Range("Table2[#All]").Copy
pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
.display
'.Send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
End Sub
 

Attachments

  • Current table.JPG
    Current table.JPG
    208.8 KB · Views: 74
  • Final email results.JPG
    Final email results.JPG
    113.1 KB · Views: 77
I may have accidentlly left out a bracket. Replace this line of code:
Code:
v = Sheets("Sheet4").Range("A2", Sheets("Sheet4").Range("A" & Rows.Count).End(xlUp)).Resize(, 12.Value
with this one;
Rich (BB code):
v = Sheets("Sheet4").Range("A2", Sheets("Sheet4").Range("A" & Rows.Count).End(xlUp)).Resize(, 12).Value
I had tried adding the bracket before but still did not work. Getting the same error on this line:

v = Sheets("Sheet4").Range("A2", Sheets("Sheet4").Range("A" & Rows.Count).End(xlUp)).Resize(, 12).Value
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Do you have a sheet named "Sheet4"?
 
Upvote 0
This is what I get with no errors when I run the version of the macro below:

Hi, As part of your Create, Modify or Terminate SOW Contingent Worker ServiceNowm requestfor the below user, you requested Remote Access for the user. Vendor Remote Access has been provisioned for this user. Please share the attached documents with the user so that they can configure their Vendor Remote Access.

RITM NumberOpenedWorker First NameWorker Last NameContract Start DateIDSoft Token File NameCost CenterDomainRemote AccessRequestorEmail
RITM #13/29/2023First_Name_1Last_Name_1
45028​
ID_1ID_1_000123456789.sdtid
3638​
TDBFGYesAndre MateusAndre.Mateus@test.com
RITM #23/29/2023First_Name_2Last_Name_2
45019​
ID_2ID_2_000123456789.sdtid
7983​
TDBFGYesAndre MateusAndre.Mateus@test.com
RITM #33/27/2023First_Name_3Last_Name_3
45009​
ID_3ID_3_000123456789.sdtid
9947​
TDBFGYesAndre MateusAndre.Mateus@test.com
RITM #43/29/2023First_Name_4Last_Name_4
45027​
ID_4ID_4_000123456789.sdtid
6084​
TDBFGYesAndre MateusAndre.Mateus@test.com


Regards,

VBA Code:
Sub CreateEmails()
    Application.ScreenUpdating = False
    Dim OutApp As Object, OutMail As Object, rng As Range, v As Variant, SigString As String, sPath As String, fName As Range
    Dim lRow As Long
    lRow = Sheets("Sheet4").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    sPath = "C:\RemoteAMBA\bin\SoftTokens\"
    SigString = Environ("appdata") & "\Microsoft\Signatures\SignatureName.htm"
    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If
    v = Sheets("Sheet4").Range("A2:A" & lRow).Resize(, 12).Value
    Set OutApp = CreateObject("Outlook.Application")
    With CreateObject("scripting.dictionary")
        For i = LBound(v) To UBound(v)
            If Not .exists(v(i, 11)) Then
                .Add v(i, 11), Nothing
                With Sheets("Sheet4")
                    .Range("A1").CurrentRegion.AutoFilter 11, v(i, 11)
                    Set rng = .AutoFilter.Range
                    Set OutMail = OutApp.CreateItem(0)
                    With OutMail
                        .To = v(i, 12)
                        .Subject = "SOW Contingent Worker - Remote Access Request"
                        .HTMLBody = "Hi, As part of your Create, Modify or Terminate SOW Contingent Worker ServiceNowm requestfor the below user, you requested Remote Access for the user. Vendor Remote Access has been provisioned for this user. Please share the attached documents with the user so that they can configure their Vendor Remote Access." _
                        & "<br><br>" & RangetoHTML(rng) & "<br><br>" & "Regards," & "<br>" & Signature
                        For Each fName In Sheets("Sheet4").Range("G2", Sheets("Sheet4").Range("G" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
                            .attachments.Add sPath & fName
                        Next fName
                        .Display
                    End With
                End With
            End If
        Next i
    End With
    Sheets("Sheet4").Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This is what I get with no errors when I run the version of the macro below:

Hi, As part of your Create, Modify or Terminate SOW Contingent Worker ServiceNowm requestfor the below user, you requested Remote Access for the user. Vendor Remote Access has been provisioned for this user. Please share the attached documents with the user so that they can configure their Vendor Remote Access.

RITM NumberOpenedWorker First NameWorker Last NameContract Start DateIDSoft Token File NameCost CenterDomainRemote AccessRequestorEmail
RITM #13/29/2023First_Name_1Last_Name_1
45028​
ID_1ID_1_000123456789.sdtid
3638​
TDBFGYesAndre MateusAndre.Mateus@test.com
RITM #23/29/2023First_Name_2Last_Name_2
45019​
ID_2ID_2_000123456789.sdtid
7983​
TDBFGYesAndre MateusAndre.Mateus@test.com
RITM #33/27/2023First_Name_3Last_Name_3
45009​
ID_3ID_3_000123456789.sdtid
9947​
TDBFGYesAndre MateusAndre.Mateus@test.com
RITM #43/29/2023First_Name_4Last_Name_4
45027​
ID_4ID_4_000123456789.sdtid
6084​
TDBFGYesAndre MateusAndre.Mateus@test.com


Regards,

VBA Code:
Sub CreateEmails()
    Application.ScreenUpdating = False
    Dim OutApp As Object, OutMail As Object, rng As Range, v As Variant, SigString As String, sPath As String, fName As Range
    Dim lRow As Long
    lRow = Sheets("Sheet4").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    sPath = "C:\RemoteAMBA\bin\SoftTokens\"
    SigString = Environ("appdata") & "\Microsoft\Signatures\SignatureName.htm"
    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If
    v = Sheets("Sheet4").Range("A2:A" & lRow).Resize(, 12).Value
    Set OutApp = CreateObject("Outlook.Application")
    With CreateObject("scripting.dictionary")
        For i = LBound(v) To UBound(v)
            If Not .exists(v(i, 11)) Then
                .Add v(i, 11), Nothing
                With Sheets("Sheet4")
                    .Range("A1").CurrentRegion.AutoFilter 11, v(i, 11)
                    Set rng = .AutoFilter.Range
                    Set OutMail = OutApp.CreateItem(0)
                    With OutMail
                        .To = v(i, 12)
                        .Subject = "SOW Contingent Worker - Remote Access Request"
                        .HTMLBody = "Hi, As part of your Create, Modify or Terminate SOW Contingent Worker ServiceNowm requestfor the below user, you requested Remote Access for the user. Vendor Remote Access has been provisioned for this user. Please share the attached documents with the user so that they can configure their Vendor Remote Access." _
                        & "<br><br>" & RangetoHTML(rng) & "<br><br>" & "Regards," & "<br>" & Signature
                        For Each fName In Sheets("Sheet4").Range("G2", Sheets("Sheet4").Range("G" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
                            .attachments.Add sPath & fName
                        Next fName
                        .Display
                    End With
                End With
            End If
        Next i
    End With
    Sheets("Sheet4").Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you very much, Sir.
I was working with two different files and "did not realize" that the file was I trying the code with had a different sheet name lol.
The code is indeed working! I really appreciate your help!
 
Upvote 0
The code is indeed working! I really appreciate your help!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

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