Jump to content

Mailing from Excel/variable storing


Recommended Posts

Purpose of script: To send emails in Outlook based on data in an excel spreadsheet. From: fields are entered for purposes of sending on behalf of (delegate), copies A2 cell from excel for To: field, Subject field is a static value and entered, returns to excel spreadsheet to read A1 and copy the first name and insert into the body of a template at an insertion point. The From, and the body of the email change based on region, so currently I have 6 different scripts that do essentially the same thing with some minor changes and want to consolidate into one script to save time.

Question:
To expedite the process of this and cut down on the amount of scripts, 6 in total I use daily, is it possible for me to somehow add the region to column C in excel, have autoit read column C values per row, and then decide which function, within a master script, to execute and loop this until there is no value in column C field?

Example spreadsheet:

Rob | rob@annuity.com |Midwest
Annie | annie@agency.com | Midwest
Kyle | kyle@agency.com | MidAtlantic
Rick | rick@megasales.com | MidAtlantic
Blank | Blank | Blank |

Example execution:
Run Birthday.au3, execute loop part through hotkey
Reads row 1, C1, value is Midwest, calls Midwest(), script runs as Midwest Birthday.au3 does currently
Reads row 2, C2, value is Midwest, calls Midwest(), script runs as Midwest Birthday.au3 does currently
Reads row 3, C3, value is MidAtlantic, calls MidAtlantic(), script runs as MidAtlantic Birthday.au3 currently
Reads row 4, C4, value is MidAtlantic, calls MidAtlantic(), script runs as MidAtlantic Birthday.au3 currently
Reads row 5, C5, value is null or blank, ends script through Exit

 

Everything I have coded in my time in AutoIt has been based mostly on mouse based movements and I don't have variable programming knowledge so I feel like I'm close to understanding how to do this, but the reading/storing variables part is beyond my current skill set. Help is appreciated. :)

Mail Merges don't work as delegated in Outlook 07, for those that might be questioning why I just don't do that.
 

MidAtlantic Birthday.au3

Midwest Birthday.au3

Edited by Revelation343
Link to comment
Share on other sites

I suggest to use the Excel UDF that comes with AutoIt plus the OutlookEX UDF which you can download from my signature.
First use _Excel_RangeRead to read the whole worksheet into an array, then loop through the array and call _OL_Wrapper_SendMail to create the mail and send it.

If needed I can provide an example script.

 

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#include <Excel.au3>
#include "OutlookEX.au3"

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test_Mail.xlsx", True) ; Open Excel workbook read-only
Global $aData = _Excel_RangeRead($oWorkbook)
_Excel_BookClose($oWorkbook, False)
_Excel_Close($oExcel, False)

$oOL = _OL_Open()

; Retrieve defined signatures
Global $aSignatures = _OL_MailSignatureGet()
; Deactivate signature for new emails
_OL_MailSignatureSet("", Default)

; Create, Modify and send mails
For $i = 0 To UBound($aData, 1) - 1
    $oItem = _OL_ItemCreate($oOL, $olMailItem, "", @ScriptDir & "\" & $aData[$i][2] & ".msg", "Subject=Test")
    ConsoleWrite("IC: " & @error & "-" & @extended & @CRLF)
    $aBody = _OL_ItemGet($oOL, $oItem, Default, "HTMLBody")
    ConsoleWrite("IG: " & @error & "-" & @extended & @CRLF)
    $sBody = StringReplace($aBody[1][1], "%Firstname%", $aData[$i][0])
    _OL_ItemModify($oOL, $oItem, Default, "HTMLBody=" & $sBody)
    ConsoleWrite("IM: " & @error & "-" & @extended & @CRLF)
    _OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, $aData[$i][1])
    ConsoleWrite("RA: " & @error & "-" & @extended & @CRLF)
    $oItem.Display()
Next

; Set signature for new emails to previous value
For $i = 1 To $aSignatures[0][0]
    If $aSignatures[$i][1] = True Then
        _OL_MailSignatureSet($aSignatures[$i][0], Default)
        ExitLoop
    EndIf
Next

_OL_Close($oOL)

First (quick & dirty) example. Reads all records from Test_mail.xlsx with the structure as defined by you.
Depending on the value in column C (MidWest, MidAtlantic) an email is created based on template MidWest.msg or MidAtlantic.msg.
Placeholder "%Firstname%" is replaced with the recipients name.
Then the mail is being displayed (parameter SentOnBehalf has not been set yet).

What you need to do:

  • Place above script in a directory
  • Create MidWest.Msg and MidAtlantic.msg in the same directory
  • Create Test_mail.xlsx in the same directory

Example MidAtlantic.msg:

Quote

Dear %FirstName%,

Happy Birthday from your friends at x y.

Signature MidAtlantic

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Ok this makes sense. Is there a way to input the From field based on column C? Or I could just make it to where the From email is in Column D if need be if that would be easier.

Edit: Nevermind, I think I got it to work here, I just added the values for the From address to the D column. :) Take a look at it and let me know if there's anything obvious I'm missing but based on my testing it looks like this is exactly the functionality I need and I have a much better understanding of how this works now.

#include <Excel.au3>
#include "OutlookEX.au3"

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\" & "Birthday.xlsx", True) ; Open Excel workbook read-only
Global $aData = _Excel_RangeRead($oWorkbook)
_Excel_BookClose($oWorkbook, False)
_Excel_Close($oExcel, False)

$oOL = _OL_Open()

; Retrieve defined signatures
Global $aSignatures = _OL_MailSignatureGet()
; Deactivate signature for new emails
_OL_MailSignatureSet("", Default)

; Create, Modify and send mails
For $i = 0 To UBound($aData, 1) - 1
    $oItem = _OL_ItemCreate($oOL, $olMailItem, "", @ScriptDir & "\" & $aData[$i][2] & ".msg", "Subject=Test", "SentOnBehalfOfName="& $aData[$i][3])
    ConsoleWrite("IC: " & @error & "-" & @extended & @CRLF)
    $aBody = _OL_ItemGet($oOL, $oItem, Default, "HTMLBody")
    ConsoleWrite("IG: " & @error & "-" & @extended & @CRLF)
    $sBody = StringReplace($aBody[1][1], "%Firstname%", $aData[$i][0])
    _OL_ItemModify($oOL, $oItem, Default, "HTMLBody=" & $sBody)
    ConsoleWrite("IM: " & @error & "-" & @extended & @CRLF)
    _OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, $aData[$i][1])
    ConsoleWrite("RA: " & @error & "-" & @extended & @CRLF)
    $oItem.Display()
Next

; Set signature for new emails to previous value
For $i = 1 To $aSignatures[0][0]
    If $aSignatures[$i][1] = True Then
        _OL_MailSignatureSet($aSignatures[$i][0], Default)
        ExitLoop
    EndIf
Next

 

Edited by Revelation343
Link to comment
Share on other sites

Looks quite good :)
Before using the script in production you could remove the ConsoleWrite statements. I inserted them for debugging reasons.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Outlook is just the frontend to a mail server like Exchange. I've never heard of a limit for Outlook. Maybe the mail server has a limit to prevent mail floods.

Next idea would be to set the date in the Excel file. This would allow to restart the script without re-sending all mails by checking the date.
Right now the created mails get displayed. If you want to send them automatically then better error checking would be needed.

What do you plan as a next step?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I like the ability to review before sending. Though in my first 3 batches of 24, there were no errors. I mean, this function is to basically pat some people on the back and isn't a high influence sort of issue. I've been out of the office for a few days so I have 249 more to send, but on the daily, this functionality should be more than sufficient. I am interested in the concept of being able to check the date incase I do have a large list like this in the future though.

 

Edit: I was able to just keep hitting send and produce all 249 at once so the hard cap that I ran into before must have only been a figment of my overworked imagination. :) 

Edited by Revelation343
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...