Jump to content

[SOLVED] How do I open an email in outlook with Autoit


Recommended Posts

Hello again everyone, I am trying to make a simple bot/AI to help me at work. I need it to read the subject line of emails, open them if a certain subject line is found, and then copy the contents of the said email and paste it in a different program. 

Can the Autoit outlook UDF help me achieve this? 

Also, I have tried the Outlook UDF and so far I have only figured out how to find all the unread emails (thanks to one of water's example scripts. @water you are a genius just saying).

I am very new to this UDF and would love any help that would guide me into making this bot/AI a reality at work.   

Edited by nooneclose
Link to post
Share on other sites
  • Moderators

@nooneclose so what have you tried on your own? Showing what code you have tried will always result in your receiving more assistance.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to post
Share on other sites

@JLogan3o13 This is all that I have looked at and tried so far. I would say 95% is just Water's code that I have copied and tested. 

#include <OutlookEX.au3>                                                ;Allows the outlook functions

AutoItSetOption('MouseCoordMode', 0)                                    ;Sets the script to accept coordinates off the program window and not the screen
AutoItSetOption('SendKeyDelay', 10)                                     ;Defines the time it takes to send text to the software/program

;Closes the script if "End" is pressed
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

HotKeySet("{END}", "Terminate")
Func Terminate()
    MsgBox($MB_ICONINFORMATION, "DEATH", "Script Stoped!")
    Exit
EndFunc

;*****************************************************************************************************************************************************************
;Script has started
;*****************************************************************************************************************************************************************
Sleep(2000)
MsgBox($MB_ICONINFORMATION, "WO_to_FTE_Bot", "Script Started!")
Sleep(1500)

; *****************************************************************************
; Create test environment
; *****************************************************************************
Global $oOutlook = _OL_Open()
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error creating a connection to Outlook. @error = " & @error & ", @extended = " & @extended)

; *****************************************************************************
; List all accounts available for the current profile
; *****************************************************************************
Global $aResult = _OL_AccountGet($oOutlook)
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_AccountGet Example Script", "Error getting list of accounts for the current profile. @error = " & @error & ", @extended = " & @extended)
_ArrayDisplay($aResult, "OutlookEX UDF: _OL_AccountGet Example Script", "", 0, "|", "|AccountType|Displayname|SMTPAddress|Username|Account object|AutoDiscoverConnectionMode|ExchangeConnectionMode|ExchangeMailboxServerName|ExchangeMailboxServerVersion")

; *****************************************************************************
; Get the first mail in the inbox and display the mail headers
; *****************************************************************************
; Access the inbox
Global $aFolder = _OL_FolderAccess($oOutlook, "", $olFolderInbox)
If @error Then Exit MsgBox(48, "", "@error = " & @error & ", @extended: " & @extended)

; Find all items in the inbox
Global $aItems = _OL_ItemFind($oOutlook, $aFolder[1], $olMail, "", "", "", "EntryID,Subject", "", 1)
If Not IsArray($aItems) Then Exit MsgBox(48, "", "@error = " & @error & ", @extended: " & @extended)

; Get the mail headers of the first mail
Global $sMailHeaders = _OL_MailheaderGet($oOutlook, $aItems[1][0])
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_MailheaderGet Example Script", "Error retrieving mail headers of mail with subject '" & $aItems[1][1] & "'. @error = " & @error & ", @extended: " & @extended)
MsgBox(64, "OutlookEX UDF: _OL_MailheaderGet Example Script", "Mail headers of mail with subject '" & $aItems[1][1] & "'." & @CRLF & @CRLF & $sMailHeaders)

;------------------------------------------------------------------------------------------------------------------------------------------------
; Example 4 - Get unread mails from a folder and all subfolders
;------------------------------------------------------------------------------------------------------------------------------------------------
$aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, "[UnRead]=True", "", "", "Subject,Body", "", 1)
If IsArray($aItems) Then
    _ArrayDisplay($aItems, "OutlookEX UDF: _OL_ItemFind Example Script - Unread mails")
Else
    MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended)
EndIf

_OL_Close($oOutlook)

I still do not know enough about this UDF, and I can not find a function to open and or read an email yet. If they do exist they either do not have clear names or do not do just those operations. Sadly it is looking like there is not a straightforward answer to my question/s or I am easily missing it because I do not understand this UDF yet.  

Again, I want to make something bot/AI that can read a subject line pick certain emails based on the subject line, open the email and copy and paste certain contents of the email into another program. (this would greatly help make a daily task for me at work easier)  

Link to post
Share on other sites

Replace _OL_MailHeaderGet with _OL_ItemGet to retrieve the subject of a mail.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

@water The code below which I changed per your request finds the first email in my inbox and displays the subject line. However, I want to read all emails one at a time and copy and past certain contents. Only the emails that are unread with a certain subject line. (subject line of "blank request")

; Get the mail headers of the first mail
Global $sMailHeaders = _OL_ItemGet($oOutlook, $aItems[1][0])
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_MailheaderGet Example Script", "Error retrieving mail headers of mail with subject '" & $aItems[1][1] & "'. @error = " & @error & ", @extended: " & @extended)
MsgBox(64, "OutlookEX UDF: _OL_MailheaderGet Example Script", "Mail headers with the subject of:'" & $aItems[1][1] & "'." & @CRLF & @CRLF & $sMailHeaders)

if I modify these lines could I get the first of those results?  

$aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, "[UnRead]=True", "", "", "Subject,Body", "", 1)
If IsArray($aItems) Then
    _ArrayDisplay($aItems, "OutlookEX UDF: _OL_ItemFind Example Script - Unread mails")
Else
    MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended)
EndIf

What I want the code to do:

1. open outlook or a connection to outlook

2. search for any unread email with a certain subject ("blank request") in a certain inbox (inbox is labeled "requests")

3. open that email and copy certain information (not all) and paste that into another program.

4. Leave that open email and search for the next unread email with the subject line of "blank request" 

5. Repeat 3, and 4 until the program is closed. 

 

Am I asking for too much from Autoit? Or am I not being clear? 

Edited by nooneclose
Link to post
Share on other sites

You have already solved 1) and 2).
2) returns all unread mails. But if you loop through the returned array you can ignore all entries with the "wrong" subject.
3) You already have the mail body in the returned array. Select the needed information from there and paste the array element to the other program
4) Why would you need to leave th processed mail open?
5) Simply to do with a loop

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

Thank you for getting back with me @water This is what I have so far.  

; *****************************************************************************
; Create test environment
; *****************************************************************************
Global $oOutlook = _OL_Open()
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error creating a connection to Outlook. @error = " & @error & ", @extended = " & @extended)


; *****************************************************************************
; List all accounts available for the current profile
; *****************************************************************************
Global $aResult = _OL_AccountGet($oOutlook)
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_AccountGet Example Script", "Error getting list of accounts for the current profile. @error = " & @error & ", @extended = " & @extended)
_ArrayDisplay($aResult, "OutlookEX UDF: _OL_AccountGet Example Script", "", 0, "|", "|AccountType|Displayname|SMTPAddress|Username|Account object|AutoDiscoverConnectionMode|ExchangeConnectionMode|ExchangeMailboxServerName|ExchangeMailboxServerVersion")


; *****************************************************************************
; Get the first mail in the inbox and display the mail headers
; *****************************************************************************
; Access the inbox
Global $aFolder = _OL_FolderAccess($oOutlook, "", $olFolderInbox)
If @error Then Exit MsgBox(48, "", "@error = " & @error & ", @extended: " & @extended)

; Find all items in the inbox
Global $aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, "", "", "", "EntryID,Subject", "", 1)
If Not IsArray($aItems) Then Exit MsgBox(48, "", "@error = " & @error & ", @extended: " & @extended)

; Get the mail headers of the first mail
Global $sMailHeaders = _OL_ItemGet($oOutlook, $aItems[1][0])
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_MailheaderGet Example Script", "Error retrieving mail headers of mail with subject '" & $aItems[1][1] & "'. @error = " & @error & ", @extended: " & @extended)
MsgBox(64, "OutlookEX UDF: _OL_MailheaderGet Example Script", "Mail headers with the subject of:'" & $aItems[1][1] & "'." & @CRLF & @CRLF & $sMailHeaders)

; Display the information and contents of the email
_ArrayDisplay($sMailHeaders, "")

_OL_Close($oOutlook)

I was able to figure out how to access the contents of the array with the _ArryDisplay (is this the correct way to do this?)

(PS. My browser glitched so please forgive the lack of format on this reply It will not let me tab or enter)

And yes so far task 1 & 2 are done. A connection is created and I find all the unread emails in the specific folder. I think I did what you said and accessed the mail body in the returned array with _Arraydisplay. (Please tell me if I did it wrong)

As for "4) Why would you need to leave the processed mail open?" I do not want to keep any of the processed emails open. I am sorry if what I said was not clear. I want to mark each processed email as read once the information has been copied and pasted and then close it and then move on to the next unread email. (does this make sense?)

As for the looping until all unread emails with the subject line of "request" have been processed and mark as read. Which loop would you suggest here?        For loop or a while?

Also, I wanted to say thank you very much for the UDF you made. Just to have the possibility of this becoming a real thing is sweet music to my ears. I'm sure I am not the only person who would say your work is more than greatly appreciated.

Edited by nooneclose
Link to post
Share on other sites

I forgot to ask this in the last post but is there a way to extend the size of the array for the body in the array? (row 9, from 0-9)

when I use _ArrayDisplay and look at the body portion it does not contain the entire body of the email. It is very close though, It only cuts off two sentences worth of info. 

I notice that row28 which holds the HTMlBody has a bigger element size. Where are these values located so that I may change them? 

Like always any help would be greatly appreciated.  

Link to post
Share on other sites

As _OL_ItemFind returns the EntryID we need to translate it to the corresponding object.

$oItem = $oOL.Session.GetItemFromID($aItems[1][0], Default) ; retrieve the items object
$oItem.Unread = False ; Set the item to read

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

According to the help information in the UDF _OL_Itemfind returns a 1-based 2D array. Means: the 0th element holds the numbr of entries (rows and columns). So the loop should look like:

For $i = 1 to $aItems[0][0]
   ; access the item using: $aItems[$i][0] 
Next

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites
$oItem = $oOL.Session.GetItemFromID($aItems[1][0], Default) ; retrieve the items object
$oItem.GetInspector
$sBody = $oItem.Body

IIRC you need to call GetInspector to retrieve the full body.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

@water This is what I have tried so far and I cannot figure out how to get and open the unread emails with a subject line of 'request'  

;------------------------------------------------------------------------------------------------------------------------------------------------
; Get/List all unread mails from the folder (MT_Inbox)
;------------------------------------------------------------------------------------------------------------------------------------------------
$aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, "[UnRead]=True", "", "", "Subject,Body", "", 1)
If IsArray($aItems) Then
    _ArrayDisplay($aItems, "OutlookEX UDF: _OL_ItemFind - Unread mails")
Else
    MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended)
EndIf
;_OL_Close($oOutlook)

Local $numberOfUnRead = UBound($aItems, $UBOUND_ROWS) - 1               ; Gets the number of unread emails

Global $emailHeaders = _OL_ItemGet($oOutlook, $aItems[1][0])
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_MailheaderGet", "Error retrieving mail headers of mail with subject '" & $aItems[1][1] & "'. @error = " & @error & ", @extended: " & @extended)
MsgBox(64, "OutlookEX UDF: _OL_MailheaderGet", "MAIL HEADERS WITH THE SUBJECT OF:" & @CRLF & "'" & $aItems[1][1] & "'." & @CRLF & @CRLF & $emailHeaders)

 does 

$aItems[1][1]

know the subject line that I am looking for?

Is it the value that is guiding the search? 

Is this code even trying to find unread emails based on their subject? I am starting to think not. 

Any help would be appreciated. 

 

Also, this code just gives me the error of "OutlookEX UDF: _OL_MailheaderGet", "Error retrieving mail headers of mail with subject '"....

 

Edited by nooneclose
Link to post
Share on other sites

@water I figured out how to search for the unread emails based on subjects. :) 

Here is my code, I also figured out where and when to input your code to find the body of the email thank you very much. 

Here is what I have so far for this portion of the code. 

;------------------------------------------------------------------------------------------------------------------------------------------------
; Get/List all unread mails from the folder (R_Inbox)
;------------------------------------------------------------------------------------------------------------------------------------------------
$aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, "[UnRead]=True", "Subject", "Request", "EntryID,Subject", "", 1)

If IsArray($aItems) Then
    _ArrayDisplay($aItems, "OutlookEX UDF: _OL_ItemFind - Unread mails")
Else
    MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended)
EndIf
;_OL_Close($oOutlook)

Local $numberOfUnRead = UBound($aItems, $UBOUND_ROWS) - 1               ; Gets the number of unread emails



;*****************************************************************************************************************************************************************
; Get the first mail in the inbox and display the emails contents
;*****************************************************************************************************************************************************************
; Access the inbox
Global $aFolder = _OL_FolderAccess($oOutlook, "", $olFolderInbox)
If @error Then Exit MsgBox(48, "", "@error = " & @error & ", @extended: " & @extended)

; Find all items in the inbox
Global $aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, "", "", "", "EntryID,Subject", "", 1)
If Not IsArray($aItems) Then Exit MsgBox(48, "", "@error = " & @error & ", @extended: " & @extended)

; Get the mail headers of the first mail
Global $sMailHeaders = _OL_ItemGet($oOutlook, $aItems[1][0])
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_MailheaderGet", "Error retrieving mail headers of mail with subject '" & $aItems[1][1] & "'. @error = " & @error & ", @extended: " & @extended)
MsgBox(64, "OutlookEX UDF: _OL_MailheaderGet", "MAIL HEADERS WITH THE SUBJECT OF:" & @CRLF & "'" & $aItems[1][1] & "'." & @CRLF & @CRLF & $sMailHeaders)


$oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) ; retrieve the items object
$oItem.GetInspector
$sBody = $oItem.Body

MsgBox("", "Did this work", $sBody)

All I have to do now is do a loop until all of the unread emails have been processed.

But before I can do that I need to figure out how to use _OL_ItemGet to open each email correct? Or is that not even necessary? 

Link to post
Share on other sites

If you mean to display a mail item then _OL_ItemGet is not needed.
If you want to access further properties then you could simply retrieve them using _OL_ITemFind (you already use it to retrieve the subject).

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

I want to pull data like this: (yes, I want to further access the contents of the email)

$oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) ; retrieve the items object
$oItem.GetInspector

$eBody = $oItem.Body                                                    ; The Body of the email
$eSenderAddress = $oItem.SenderEmailAddress                             ; Customers email address
$eSentOn        = $oItem.SentOn                                         ; When was the email sent?
$eSubject       = $oItem.Subject                                        ; Subject of the email

from the email. 

Also in the body, it is possible to break the information up into pieces on certain words: (see attached image)

I want to take parts of the body and store each of those different bits of information into variables.

like:

$eDescription = ( The email body Description )

$eComments = ( The email body Additional comments )  

Does this make sense?

I would like to take everything after the word "description" and before the word "Additional" and store that string into a variable named "$eDescription"

Once I finish this part I can begin coding the loop process. 

Also thank you for your help so far. I know you have answered a lot of my questions and I must seem annoying at this point.

So thanks for sticking with me during this experience. 

Screenshot (134).png

Link to post
Share on other sites

Splitting the mailbody into pieces could be a task for StringRegExp (regular expression).

Or use Stringsplit with separator @CRLF or @LF (whichever applies) to split the mailbody into an Array of single lines.
Then loop through the array and check for lines starting with "Description:". Grab the rest of the line using StringMid and all following lines as long as they do not start with "Additional comments:"

Can't post runnable code as I do not have access to a Windows System a the emoment.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

@water I figured out how to break up the body and get the information I wanted...by accident. I'm glad I am able to get the info. It is just that I don't actually understand how my code works. Can you please explain this to me?

;*******************************************************************************
; Gets the Description from the email body
;*******************************************************************************
$eParts1 = StringSplit($eBody, "Description:", $STR_ENTIRESPLIT)
_ArrayDisplay($eParts1, "Pass 1")

$eParts2 = StringSplit($eParts1[2], "Additional" , $STR_ENTIRESPLIT)
_ArrayDisplay($eParts2, "Pass 2")

$eDescription = StringSplit($eParts2[1], "Reported" , $STR_ENTIRESPLIT)
_ArrayDisplay($eDescription, "Description")


;*******************************************************************************
; Gets the Comments from the email body
;*******************************************************************************
$eParts1 = StringSplit($eBody, "comments:", $STR_ENTIRESPLIT)
_ArrayDisplay($eParts1, "Pass 1")

$eParts2 = StringSplit($eParts1[2], "Location" , $STR_ENTIRESPLIT)
_ArrayDisplay($eParts2, "Pass 2")

$eComments = StringSplit($eParts2[1], "Address" , $STR_ENTIRESPLIT)
_ArrayDisplay($eComments, "Comments")

The output of both of those is just what I needed. (you can reference the image I posted last time to see what I started with)

 

So I start with this whole body then I split at one word in pass 1.

Then by Pass 2, a little more is cut out leaving some words behind that I don't want.

Then by Pass 3 everything I don't want is cut out and what I want is left.  

 

Also is there a more efficient way to achieve this? 

Getting closer to being done. Feels good, thanks for your help with this. 😊

Edited by nooneclose
Link to post
Share on other sites

Glad you got it running :)

Using StringSplit the way you do is one of the many ways to solve your problem. Any other solution might be a bit faster but could be much more complex (like regular expression).

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Hermes
      Hi, I am struggling in setting the value of a textarea based on the value of clipboard (that contains a long web page source codes). If I use _WD_SetElementValue, it freezes after some time, or appears to be pressing tab and goes out of focus. I can also use send keys but i need the script to run in the background.
      Here is the full script:
      #Include "Chrome.au3" #Include "wd_core.au3" #Include "wd_helper.au3" #Include "WinHttp.au3" #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Array.au3> #include <AutoItConstants.au3> #include <WinAPIFiles.au3> #include <GDIPlus.au3> #include <Excel.au3> Local $sDesiredCapabilities, $sSession SetupChrome() _WD_Startup() $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_LoadWait($sSession) _WD_Navigate($sSession, "http://demo.borland.com/testsite/stadyn_largepagewithimages.html") _WD_LoadWait($sSession) Global $sSource = _WD_GetSource($sSession) Local $Paste = ClipPut($sSource) Local $sData = ClipGet() Local $aArray = 0, _ $iOffset = 1 While 1 $aArray = StringRegExp($sData, '(?s)<p>.*</p>', $STR_REGEXPARRAYMATCH, $iOffset) If @error Then ExitLoop $iOffset = @extended For $i = 0 To UBound($aArray) - 1 Local $Paste = ClipPut($aArray[$i]) Local $sRegExData = ClipGet() ;MsgBox(0, "", "$sRegExData = " & $sRegExData) Next WEnd _WD_Navigate($sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_textarea_placeholder") _WD_WaitElement($sSession, $_WD_LOCATOR_ByCSSSelector, "iframe#iframeResult") Local $sElement1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "iframe#iframeResult") _WD_FrameEnter($sSession, $sElement1) _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//html/body/textarea") $textarea = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//html/body/textarea") _WD_ElementAction($sSession, $textarea, 'click') ;WD SetElementValue(SsSession, Stextarea, $sRegExData) <-- I can do this but the focus goes out, or the browser freezes _WD_FrameLeave($sSession) sleep(2000) Send("^v") _WD_LoadWait($sSession) _WD_Shutdown() Func SetupChrome() _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}' EndFunc ;==>SetupChrome Can someone help me please, or re-direct me to the right path? TIA!
    • By water
      Extensive library to control and manipulate Microsoft Outlook. This UDF holds the functions to automate items (folders, mails, contacts ...) in the background. Can be seen like an API.
      There are other UDFs available to automate Outlook:
      OutlookEX_GUI: This UDF holds the functions to automate the Outlook GUI. OutlookTools: Allows to import/export contacts and events to VCF/ICS files and much more.  Threads: Development - General Help & Support - Example Scripts - Wiki
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2020-02-09)
      None
    • By Epulone
      For a specific requirement the outbound email are managed by an on-prem EDGE server. The EDGE re-write the local on-prem email address with another domain [exposed domain - mx record-]. Everything is working fine but we are having an issue when the internal local users are requesting an invite meeting by  Outlook calendar . In that case when the receiver accept the request invite it generate an error "domain not found". In this scenario the EDGE server is not able to rewrite also the calendar MIME request  (V-card) header inside the message so when the receiver accepts the request the reply [mechanism] will fails as will be used the email address of the local user  [domain not exposed ] and not the external one.
      I was wondering if Autoit is able to manipulate somehow the invite heade- calendar MIME (V-card))- re-witing the user email.   Thanks    
    • By DJ143
      I have a autoit exe file which is used in upload/browse file functionality.  This has been integrated with selenium framework and I am invoking the autoit exe using Java process and runtime. 
      Now the issue is when I run the scripts and invoke the autoit exe in local it works perfectly.  But when I use selenium grid or jenkins to run the scripts in another windows server it is not working.
      Can anyone please suggest any solution for this?
    • By Hermes
      Hello, the script below will read column A from an excel file - and if a value matches in the browser, it will click the corresponding link and click on a specific button to paste the data, then writes "Completed" in Column B. It will continue to read from the excel file and do the same thing for all the remaining rows.
      #Include "Chrome.au3" #Include "wd_core.au3" #Include "wd_helper.au3" #Include "WinHttp.au3" #include <MsgBoxConstants.au3> #include <File.au3> #include <IE.au3> #include <Array.au3> #include <INet.au3> #include <AutoItConstants.au3> #include <WinAPIFiles.au3> #include <GDIPlus.au3> #include <Excel.au3> #Include "WinHttp.au3" #Include "_HtmlTable2Array.au3" Local $sDesiredCapabilities, $sSession SetupChrome() _WD_Startup() $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_LoadWait($sSession) _WD_Navigate($sSession, "table1.html") _WD_LoadWait($sSession) _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//table[@class='main']") Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//table[@class='main']") ;ConsoleWrite ("mat-table " & $sElement & @CRLF) Local $aArray1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, ".//td[contains(@class,'data')]", $sElement, True) sleep(1000) For $i = 0 to UBound($aArray1) - 1 $aArray1[$i] = _WD_ElementAction($sSession, $aArray1[$i], 'text') Next ;_ArrayDisplay($aArray1) ;Email variables $SmtpServer = "" ; address for the smtp-server to use - REQUIRED $FromName = "Hermes" ; name from who the email was sent $FromAddress = "sender@gmail.com" ; address from where the mail should come $ToAddress = "recipient@gmail.com" ; destination address of the email - REQUIRED, use commas (,) to add more email addresses $Subject = "File not found" ; subject from the email - can be anything you want it to be $Body = "File not found!" ; the messagebody from the mail - can be left blank but then you get a blank mail $AttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed $CcAddress = "" ; address for cc - leave blank if not needed $BccAddress = "" ; address for bcc - leave blank if not needed $Importance = "High" ; Send message priority: "High", "Normal", "Low" $Username = "" ; username for the account used from where the mail gets sent - REQUIRED $Password = "" ; password for the account used from where the mail gets sent - REQUIRED $IPPort = 25 ; port used for sending the mail $ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS $tls = 0 ; enables/disables TLS when required Local $oAppl = _Excel_Open() Local $sWorkbook = "c:\test.xlsx" Local $oWorkbook = _Excel_BookOpen($oAppl, $sWorkbook) ;open excel and pass both parameters If FileExists($sWorkbook) Then ;Check if the file exist. Local $oAppl = _Excel_Open() Local $sWorkbook = "c:\test.xlsx" Local $oWorkbook = _Excel_BookOpen($oAppl, $sWorkbook) ;open excel and pass both parameters Local $aArray2 = _Excel_RangeRead($oWorkbook,Default,$oWorkbook.ActiveSheet.Usedrange.Columns("A:A")) Local $iIdx Local $Skipline = 0 ;0==> first line Do Local $temprf For $i = 0 To UBound($aArray2) - 1 $temprf &= $aArray2[$i] _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, ".//a[contains(@class,'edit') and contains(text(),'Edit')]") Local $aElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, ".//a[contains(@class,'edit') and contains(text(),'Edit')]", $sElement, True) $iIdx = _ArraySearch($aArray1, $aArray2[$i]) If @error Then ContinueLoop _WD_ElementAction($sSession, $aElement[$iIdx], 'click') If $i < $Skipline Then ContinueLoop $oRange = $oWorkbook.ActiveSheet.Range("B" & $i + 1 & ":XFD" & $i + 1) _Excel_RangeCopyPaste($oWorkbook.Activesheet, $oRange) ;Paste Local $oTest4 = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "pastebutton") _WD_ElementAction($sSession, $oTest4, 'click') Sleep(1000) ;Save Button Local $save3 = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "button.button") _WD_ElementAction($sSession, $save3, 'click') _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "Completed", "B" & $i+1) sleep(1000) Next Until (Not @error) _Excel_Close($oWorkbook) Else _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl, $tls) Exit EndIf _WD_LoadWait($sSession) ;Attaching files to emails Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0, $tls = 0) Local $objEmail = ObjCreate("CDO.Message") $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>' $objEmail.To = $s_ToAddress Local $i_Error = 0 Local $i_Error_desciption = "" If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress $objEmail.Subject = $s_Subject If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then $objEmail.HTMLBody = $as_Body Else $objEmail.Textbody = $as_Body & @CRLF EndIf $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer If Number($IPPort) = 0 then $IPPort = 25 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort ;Authenticated SMTP If $s_Username <> "" Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password EndIf ; Set security params If $ssl Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True If $tls Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendtls") = True ;Update settings $objEmail.Configuration.Fields.Update ; Set Email Importance Switch $s_Importance Case "High" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High" Case "Normal" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal" Case "Low" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low" EndSwitch $objEmail.Fields.Update ; Sent the Message $objEmail.Send $objEmail="" EndFunc ;==>_INetSmtpMailCom Local $aDir = _FileListToArrayRec(@TempDir, "scoped_dir*;chrome_*", $FLTAR_FOLDERS, $FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) Sleep(2000) For $i = 1 To $aDir[0] DirRemove($aDir[$i], $DIR_REMOVE) Next _WD_LoadWait($sSession) _WD_Shutdown() Func SetupChrome() _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}' EndFunc ;==>SetupChrome If the excel file doesn't exists in the folder, it will send an email to a specific recipient.
      What i am trying figure out now is if the excel crashes while the script/loop is running, I want to relaunch the excel file continue to the last row before the excel crashed. So if the value of column B is not marked as "completed", it should continue from that row
      Appreciate any help that I can get to achieve this.
      table1.html test.xlsx
×
×
  • Create New...