Jump to content
Sign in to follow this  
randym

Appending text to an Outlook Email Subject

Recommended Posts

randym

I have an accounting system that generates emails to our customers. It creates each email individually and brings up the Outlook window for each one. The user is intended to review the email and then click 'send'

This is a daily process with many emails, so I have successfully created an Autoit script to detect the Outlook email window and to send the hot key the sends the email.

So far, so good - however, the accounting program also populates the Subject field of the email and what I want to do is append a text string to the end of the existing text. Not having much luck. I want to avoid 'clicking' into the field due to the fact that I do not always know exactly where it will be on the desktop.

Using info and recorder, I have the following so far... note that the text appending code does not work, but the email does send...

#region ---Au3Recorder generated code Start (v3.3.7.0) ---
#region --- Internal functions Au3Recorder Start ---

Func _Au3RecordSetup()
Opt('WinWaitDelay', 100)
Opt('WinDetectHiddenText', 1)
Opt('MouseCoordMode', 0)
EndFunc ;==>_Au3RecordSetup

Func _WinWaitActivate($title, $text, $timeout = 0)
WinWait($title, $text, $timeout)
If Not WinActive($title, $text) Then WinActivate($title, $text)
WinWaitActive($title, $text, $timeout)
EndFunc ;==>_WinWaitActivate

_AU3RecordSetup()

#endregion --- Internal functions Au3Recorder Start ---
While 1
_WinWaitActivate("Invoice # ", "MSO Generic Control ")
Local $sText = ControlGetText("Invoice # ", "", 4101)
$sText = $sText & " - From Company Name, Inc."
ControlSetText("Invoice # ", "", 4101, $sText, 1)
Sleep (500)
ControlClick("Invoice # ", "", "[TEXT:&Send]", "left", 1)
Sleep(500)
WEnd
#endregion ---Au3Recorder generated code Start (v3.3.7.0) ---

This is the window info I have obtained in the attached post-76961-0-70411100-1363282782_thumb.j... Any help would be greatly appreciated


And the sign said 'Long Hairded Creepy People Need Not Apply' ... So I stuffed my hair up under my hat and I went in to ask him why?

Share this post


Link to post
Share on other sites
kylomas

This is a daily process with many emails, so I have successfully created an Autoit script to detect the Outlook email window and to send the hot key the sends the email.

And the user does NOT need to review the email?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
water

What do you mean by "... does not work ..."? Do you get an empty subject or doesn't it change the subject at all or ...?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
randym

kylomas - no, the user does not need to review the email


And the sign said 'Long Hairded Creepy People Need Not Apply' ... So I stuffed my hair up under my hat and I went in to ask him why?

Share this post


Link to post
Share on other sites
kylomas

Search the forum. There are many examples of sending email.

Good Luck,

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
water

kylomas,

as I understand the OP the mail is already generated and displayed by another application. He needs a script that changes the subject and sends the mail.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
randym

Water - The original subject is unchanged


And the sign said 'Long Hairded Creepy People Need Not Apply' ... So I stuffed my hair up under my hat and I went in to ask him why?

Share this post


Link to post
Share on other sites
water

Can you please add the following line after

Local $sText = ControlGetText("Invoice # ", "", 4101)
MsgBox(0, "", "@error = " & @error & @CRLF & "$sText = " & $sText)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
kylomas

@water - Yes, the title and some of the info is contradictory. Was hoping that the email was generated "somewhere" as text and could just be sent using SMTP.


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
randym

@water - sorry about the delay getting back to this... got called away on another issue. I have added the line of code you sent and got the following...

post-76961-0-59362500-1363352765_thumb.j

@kylomas - I would love to use one of my own email tools, although I have yet to write one using Autoit. Unfortunately, I am stuck with what the app gives me.


And the sign said 'Long Hairded Creepy People Need Not Apply' ... So I stuffed my hair up under my hat and I went in to ask him why?

Share this post


Link to post
Share on other sites
water

So retrieving the text seems to work.

Now we need to test if setting the text returns an error. Can you insert

MsgBox(0, "Error", "@error = " & @error)
after
ControlSetText("Invoice # ", "", 4101, $sText, 1)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
randym

That message box returned

@error = 0


And the sign said 'Long Hairded Creepy People Need Not Apply' ... So I stuffed my hair up under my hat and I went in to ask him why?

Share this post


Link to post
Share on other sites
water

This little script works fine here (Windows 7, Office 2010) - modify the lines marked with "<== modify":

Func _Au3RecordSetup()
    Opt('WinWaitDelay', 100)
    Opt('WinDetectHiddenText', 1)
EndFunc   ;==>_Au3RecordSetup

Func _WinWaitActivate($title, $text, $timeout = 0)
    WinWait($title, $text, $timeout)
    If Not WinActive($title, $text) Then WinActivate($title, $text)
    WinWaitActive($title, $text, $timeout)
EndFunc   ;==>_WinWaitActivate

_AU3RecordSetup()

Global $sWindowTitle = "Test"   ; <== modify
Global $sButtonText = "&Senden" ; <== modify
_WinWaitActivate($sWindowTitle, "MSO Generic Control ")
Local $sText = ControlGetText($sWindowTitle, "", 4101)
$sText = $sText & " - From Company Name, Inc."
ControlSetText($sWindowTitle, "", 4101, $sText, 1)
Sleep(500)
ControlClick($sWindowTitle, "", "[TEXT:" & $sButtonText & "]", "left", 1)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
randym

@water - I want to thank you for all your assistance in this matter and to show you what my final results worked out to be.

The code you sent last did work, at least it seemed so on the surface... The subject on the screen was changed and displayed correctly. Even a ControlGetText() returned the properly modified subject, but for some reason, Outlook never included the changes when the message was sent.

Func _Au3RecordSetup()
Opt('WinWaitDelay', 100)
Opt('WinDetectHiddenText', 1)
EndFunc ;==>_Au3RecordSetup

Func _WinWaitActivate($title, $text, $timeout = 0)
WinWait($title, $text, $timeout)
If Not WinActive($title, $text) Then WinActivate($title, $text)
WinWaitActive($title, $text, $timeout)
EndFunc ;==>_WinWaitActivate

_AU3RecordSetup()

Global $sWindowTitle = "Invoice # *" ; <== modify
Global $sButtonText = "&Send" ; <== modify
_WinWaitActivate($sWindowTitle, "MSO Generic Control ")
ControlFocus($sWindowTitle, "", 4101)
Send("{END}{SPACE}-{SPACE}{SHIFTDOWN}C{SHIFTUP}ompany{SPACE}{SHIFTDOWN}N{SHIFTUP}ame,{SPACE}{SHIFTDOWN}I{SHIFTUP}nc.{TAB}")
Sleep(500)

;Local $sText = ControlGetText($sWindowTitle, "", 4101)
;MsgBox(0, "", "@error = " & @error & @CRLF & "$sText after Send = " & $sText)

ControlClick($sWindowTitle, "", "[TEXT:" & $sButtonText & "]", "left", 1)

Although I ended up bringing the subject control into focus and sending keystrokes to the end of the line, the secret really seemed to be that Outlook wanted a {TAB} when I was done. It seemed to recognize the subject changes at that point and send them on properly. Just one of the many oddities in Outlook I guess.


And the sign said 'Long Hairded Creepy People Need Not Apply' ... So I stuffed my hair up under my hat and I went in to ask him why?

Share this post


Link to post
Share on other sites
water

Glad you've found a solution.

The next step I would have suggested would be to access the mail item with my OutlookEX UDF, modify it and then send it.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Using my OutlookEX UDF this solution is even more reliable:

#include <OutlookEX.au3>
; Connect to Outlook
Global $oOutlook = _OL_Open()
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error creating a connection to Outlook. @error = " & @error & ", @extended = " & @extended)
; Get current item
$oItem = $oOutlook.ActiveInspector.CurrentItem
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error accessing current Inspector. @error = " & @error & ", @extended = " & @extended)
; Get Subject property
$aProperties = _OL_ItemGet($oOutlook, $oItem, Default, "Subject")
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error accessing current item. @error = " & @error & ", @extended = " & @extended)
; Modify Subject property
$sSubject = $aProperties[1][1] & " - Changed Subject"
_OL_ItemModify($oOutlook, $oItem, Default, "Subject=" & $sSubject)
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error accessing current item. @error = " & @error & ", @extended = " & @extended)
; Send item
_OL_ItemSend($oOutlook, $oItem)
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error sending current item. @error = " & @error & ", @extended = " & @extended)
; Close connection to Outlook
_OL_Close($oOutlook)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
randym

@water - Thank you for the sample script using your OutlookEX UDF - I have gotten it downloaded and installed and the script up and running - I am in the process of re-writing my script to take advantage of these functions. I will let you know how it goes.


And the sign said 'Long Hairded Creepy People Need Not Apply' ... So I stuffed my hair up under my hat and I went in to ask him why?

Share this post


Link to post
Share on other sites
water

If you have any questions I will be happy to answer them.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
randym

@water - Had a little scope creep here - had to adapt the script to query the database and pull out a PO# and add it to the subject as well as the company name. I searched the forum and found some examples that helped me get that done. Adding in your solution w/ OutlookEX UDF has made things work really well.

This script was originally intended to be compiled and put in the Startup folder to run all the time waiting for emails to send. I have changed that thinking and have instructed the staff to start the script at the time they are generating the emails and shut if when done. However, I did have a question perhaps there is a better method to accomplish waiting for a specific window than the way I did it.

First, I tried using the _WinWaitActivate() function the recorder provided. After a fashion, it did work, but with problems. I needed to match the first 11 characters of the window title. I changed the script to look for those 11 characters using the default WinTitleMatchMode match from start. Trouble is my title "Invoice # *" was also matched by "Inbox". At least I think that was the problem. In writing this I wonder if match mode 2 match any substring would have been a better choice?

Anyway to go around this I did the following - Note the If statement in the while loop

Global $sWindowTitle = "Invoice # *"
Global $CompanyName = " - Company Name"
Local $sTitle
While 1
$sTitle = WinGetTitle("[CLASS:rctrl_renwnd32]", "")
If StringInStr($sTitle, $sWindowTitle) = 1 And ControlFocus($sTitle, "", 4256) = 1 Then
_ProcessEmail($sTitle)
EndIf
Sleep(500)
WEnd

Using StringInStr() to match the first 11 exactly - is there a better way to accomplish this?

Secondly - It became apparent that I needed to be able to tell the status of an email... whether it had been sent previously or not. Reading through previously sent emails with the script still running caused some excitement around here for a few minutes. Outlook seems to know status because a new email has a 'Send' button, but reading an old email does not. Not sure if that is procedural or data driven. Anyway,I used the ControlFocus() function to test for the existence of the 'Send' button and processed the email only when it exists. I guess my question is, are there any recommendations you would make to determine the email state?


And the sign said 'Long Hairded Creepy People Need Not Apply' ... So I stuffed my hair up under my hat and I went in to ask him why?

Share this post


Link to post
Share on other sites
water

Another possibility is to use Outlook events. Your script could be started from the Startup folder and will be triggered every time a user clicks on the send button of an Outlook item. A function gets called which then can inspect the type of item etc. If it is a mail item you want to modify, it changes the subject and lets Outlook send the mail.

I will try to post an example tomorrow.

Which Outlook version do you use?

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  

×