Jump to content

OutlookEX.au3" (2443) : ==> Subscript used on non-accessible variable.:


Recommended Posts

Can some one please point me why i keep getting the error while the script is in loop:-

"C:\Program Files (x86)\AutoIt3\Include\OutlookEX.au3" (2443) : ==> Subscript used on non-accessible variable.:
If Not (IsNumber($iAttachment)) Or $iAttachment < 1 Or $iAttachment > $aAttachments[0][0] Then Return SetError(8, $aAttachments[0][0], 0)
If Not (IsNumber($iAttachment)) Or $iAttachment < 1 Or $iAttachment > $aAttachments^ ERROR

My code as below though copied and modified as per need, to get attachments, and other details.

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StringConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>
#include <String.au3>
#include <UIAWrappers.au3>
#include <GuiEdit.au3>
#include <Clipboard.au3>
#include <WinAPI.au3>
#include <ColorConstants.au3>
#include <FontConstants.au3>
#include <ProgressConstants.au3>
#include <Restart.au3>
#include <_IEquerySelectorAll.au3>
#include <GuiRichEdit.au3>
#include <hiddenDesktopInteract.au3>
#include <Date.au3>
#include <CUIAutomation2.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <OutlookEX.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

;Global $oErrorHandler = ObjEvent("Autoit.Error", "ComErrorHandler")

FileDelete("D:\attachment\*.*")
FileDelete("D:\Email-Details\*.*")
FileDelete("D:\Script-temp\*.*")
FileDelete("D:\Downloaded-Cert\*.*")
FileDelete("D:\LOG\*.*")
RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)

;Global $Errorcomments
;If Not IsDeclared($Errorcomments) Then Global $Errorcomments

; *****************************************************************************
; Example Script
; Handle Outlook NewmailEX event when a new mail arrives.
; This script loops until Shift-Alt-E is pressed to exit.
; *****************************************************************************
HotKeySet("+!e", "_Exit") ;Shift-Alt-E to Exit the script
;MsgBox(64, "OutlookEX UDF Example Script", "Hotkey to exit the script: 'Shift-Alt-E'!")



Global $oOApp = ObjCreate("Outlook.Application")
Global $test = ObjEvent($oOApp, "oOApp_")

While 1
    Sleep(400)
WEnd

FileDelete("D:\attachment\*.*")
FileDelete("D:\Email-Details\*.*")
FileDelete("D:\Script-temp\*.*")
FileDelete("D:\Downloaded-Cert\*.*")
FileDelete("D:\LOG\*.*")
RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)

; Outlook 2007 - NewMailEx event - http://msdn.microsoft.com/en-us/library/bb147646%28v=office.12%29.aspx
Func oOApp_NewMailEx($sOL_EntryId)

    FileDelete("D:\attachment\*.*")
    FileDelete("D:\Email-Details\*.*")
    FileDelete("D:\Script-temp\*.*")
    FileDelete("D:\Downloaded-Cert\*.*")
    FileDelete("D:\LOG\*.*")
    RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)
    Sleep(1000)
    Local $oOL_Item = $oOApp.Session.GetItemFromID($sOL_EntryId, Default)
    Sleep(1000)
    _OL_ItemAttachmentSave($oOApp, $oOL_Item, Default, 1, "D:\attachment\")
    Sleep(1000)
    ;MsgBox(64, "OutlookEX UDF Example Script", "New mail has arrived!" & @CRLF & @CRLF & _
    ;"From:    " & $oOL_Item.SenderName & @CRLF & _
    ;"Subject: " & $oOL_Item.Subject)
    Local $oOL_Item1 = $oOApp.Session.GetItemFromID($sOL_EntryId, Default)
    Sleep(1000)
    Local $oOL_Sender = $oOL_Item1.Sender

    If $oOL_Sender.AddressEntryUserType = $olExchangeUserAddressEntry Or $oOL_Sender.AddressEntryUserType = $olExchangeRemoteUserAddressEntry Then
        Local $oExchangeUser = $oOL_Sender.GetExchangeUser
    Else
        Sleep(2000)
    EndIf




    Sleep(1000)

    Local $sendername = ("D:\Email-Details\sendername.txt")
    Local $Subject = ("D:\Email-Details\subject.txt")

    ;=================================Subjectfile file write ===========================================
    Local $Subject1 = FileOpen($Subject, $FO_APPEND)
    If $Subject1 = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the Subject file.")
        Return False
    EndIf

    ; Write data to the file using the handle returned by FileOpen.
    FileWrite($Subject1, $oOL_Item1.Subject)

    ; Close the handle returned by FileOpen.
    FileClose($Subject1)
    Sleep(1000)



;=================================End sendername file write ===========================================
    $Filerunpath = ("C:\Users\certauto\Desktop\Test.Exe")
    Local $iPID = Run($Filerunpath)
    ProcessWaitClose($iPID)
    Sleep(1000)

    _OL_ItemModify($oOApp, $oOL_Item, Default, "Unread=False")
    ;$openemail = WinActivate ( "[CLASS:NetUIHWND; INSTANCE:1]", "" )

    ;ControlClick($openemail, "", "NUIDialog")
    ;$oOApp.Session.Logoff
    Sleep(1000)


    ;====================Mark email as read===========================================


    ;_OL_ItemModify($oOL_Item,$oOApp.Session.GetItemFromID($sOL_EntryId, Default, "Unread=False")


    Sleep(1000)


EndFunc   ;==>oOApp_NewMailEx


Func _Exit()
    Exit
EndFunc   ;==>_Exit
Edited by Cyborg5000
Link to comment
Share on other sites

This error shows up when an email has no attachments. Function _OL_ItemAttachmentSave calls _OL_ItemAttachmentGet to get a list of attachments. If there are none @error is set to 3. Unfortunately _OL_ItemAttachmentSave does not check @error. So this is a bug.

Will provide a solution tomorrow.

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

Please add the marked line to OutlookEX.au3:

Func _OL_ItemAttachmentSave($oOL, $vItem, $sStoreID, $iAttachment, $sPath, $iFlags = 0)
    Local $sDrive, $sDir, $sFName, $sExt
    If StringStripWS($sPath, BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)) = "" Then Return SetError(1, 0, 0)
    _PathSplit($sPath, $sDrive, $sDir, $sFName, $sExt)
    If Not IsObj($vItem) Then
        If StringStripWS($vItem, BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)) = "" Then Return SetError(6, 0, 0)
        $vItem = $oOL.Session.GetItemFromID($vItem, $sStoreID)
        If Not IsObj($vItem) Then Return SetError(3, 0, 0)
    EndIf
    If $vItem.Attachments.Count = 0 Then Return SetError(9, 0, 0) ; <== ADD THIS LINE

 

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

Hi @water

 

Update the line and also copied and pasted the above code to outlookex file.

However now getting 

"C:\Program Files (x86)\AutoIt3\Include\OutlookEX.au3" (2441) : ==> The requested action with this object has failed.:
If $vItem.Attachments.Count = 0 Then Return SetError(9, 0, 0)
If $vItem^ ERROR

Edited by Cyborg5000
Link to comment
Share on other sites

Which version of the UDF do you run?
In my latest version the line number is 2080 not 2441.

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

It just tells that something went wrong.
To get detailed error information add

_OL_ErrorNotify(2)

before calling the function.

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

Thanks @water

Sharing my code, do you see a fault?

Iam just trying to keep the script check all the email's that comes to the inbox folder for an event to trigger the rest of the functions...

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StringConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>
#include <String.au3>
#include <UIAWrappers.au3>
#include <GuiEdit.au3>
#include <Clipboard.au3>
#include <WinAPI.au3>
#include <ColorConstants.au3>
#include <FontConstants.au3>
#include <ProgressConstants.au3>
#include <Restart.au3>
#include <_IEquerySelectorAll.au3>
#include <GuiRichEdit.au3>
#include <hiddenDesktopInteract.au3>
#include <Date.au3>
#include <CUIAutomation2.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <OutlookEX.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

;Global $oErrorHandler = ObjEvent("Autoit.Error", "ComErrorHandler")

FileDelete("D:\attachment\*.*")
FileDelete("D:\Email-Details\*.*")
FileDelete("D:\Script-temp\*.*")
FileDelete("D:\Downloaded-Cert\*.*")
FileDelete("D:\LOG\*.*")
RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)

;Global $Errorcomments
;If Not IsDeclared($Errorcomments) Then Global $Errorcomments

; *****************************************************************************
; Example Script
; Handle Outlook NewmailEX event when a new mail arrives.
; This script loops until Shift-Alt-E is pressed to exit.
; *****************************************************************************
HotKeySet("+!e", "_Exit") ;Shift-Alt-E to Exit the script
;MsgBox(64, "OutlookEX UDF Example Script", "Hotkey to exit the script: 'Shift-Alt-E'!")



Global $oOApp = ObjCreate("Outlook.Application")
Global $test = ObjEvent($oOApp, "oOApp_")

While 1
    Sleep(400)
WEnd

FileDelete("D:\attachment\*.*")
FileDelete("D:\Email-Details\*.*")
FileDelete("D:\Script-temp\*.*")
FileDelete("D:\Downloaded-Cert\*.*")
FileDelete("D:\LOG\*.*")
RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)

; Outlook 2007 - NewMailEx event - http://msdn.microsoft.com/en-us/library/bb147646%28v=office.12%29.aspx
Func oOApp_NewMailEx($sOL_EntryId)
    Global $sMailbox = "certauto@xyz.com\Inbox"
    FileDelete("D:\attachment\*.*")
    FileDelete("D:\Email-Details\*.*")
    FileDelete("D:\Script-temp\*.*")
    FileDelete("D:\Downloaded-Cert\*.*")
    FileDelete("D:\LOG\*.*")
    RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)
    Sleep(1000)
    Global $aFolder = _OL_FolderAccess($oOApp, $sMailbox)
    ;If @error Then Exit MsgBox($MB_IconError, $sTitle, "Error when calling _OL_FolderAccess: @error=" & @error & ", @extended=" & @extended & @CRLF)

    Local $oOL_Item = $oOApp.Session.GetItemFromID($sOL_EntryId, Default)
    Sleep(1000)
    _OL_ErrorNotify(2)
    _OL_ItemAttachmentSave($aFolder, $oOL_Item, Default, 1, "D:\attachment\")
    Sleep(1000)
    ;MsgBox(64, "OutlookEX UDF Example Script", "New mail has arrived!" & @CRLF & @CRLF & _
    ;"From:    " & $oOL_Item.SenderName & @CRLF & _
    ;"Subject: " & $oOL_Item.Subject)
    Local $oOL_Item1 = $aFolder.Session.GetItemFromID($sOL_EntryId, Default)
    Sleep(1000)
    Local $oOL_Sender = $oOL_Item1.Sender

    If $oOL_Sender.AddressEntryUserType = $olExchangeUserAddressEntry Or $oOL_Sender.AddressEntryUserType = $olExchangeRemoteUserAddressEntry Then
        Local $oExchangeUser = $oOL_Sender.GetExchangeUser
    Else
        Sleep(2000)
    EndIf




    Sleep(1000)

    Local $sendername = ("D:\Email-Details\sendername.txt")
    Local $Subject = ("D:\Email-Details\subject.txt")

    ;=================================Subjectfile file write ===========================================
    Local $Subject1 = FileOpen($Subject, $FO_APPEND)
    If $Subject1 = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the Subject file.")
        Return False
    EndIf

    ; Write data to the file using the handle returned by FileOpen.
    FileWrite($Subject1, $oOL_Item1.Subject)

    ; Close the handle returned by FileOpen.
    FileClose($Subject1)
    Sleep(1000)


    ;=================================sendername file write ===========================================



    _ReplaceStringInFile($Subject, "Certifcate Request - ", "")
    _ReplaceStringInFile($Subject, " -- ", @CRLF)

    Local $hFileOpen = FileOpen($Subject, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")

    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen, 2)
    FileWrite($sendername, $sFileRead)
    _ReplaceStringInFile($Subject, $sFileRead, "")
    FileClose($hFileOpen)
    Sleep(2000)
    ;=================================End sendername file write ===========================================
    $Filerunpath = ("D:\NOC script\Test.Exe")
    Local $iPID = Run($Filerunpath)
    ProcessWaitClose($iPID)
    Sleep(1000)

    _OL_ItemModify($oOApp, $oOL_Item, Default, "Unread=False")
    ;$openemail = WinActivate ( "[CLASS:NetUIHWND; INSTANCE:1]", "" )


    ;ControlClick($openemail, "", "NUIDialog")
    ;$oOApp.Session.Logoff
    Sleep(1000)


    ;====================Mark email as read===========================================


    ;_OL_ItemModify($oOL_Item,$oOApp.Session.GetItemFromID($sOL_EntryId, Default, "Unread=False")


    Sleep(1000)


EndFunc   ;==>oOApp_NewMailEx


Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

Edited by Cyborg5000
Link to comment
Share on other sites

Yes.

_OL_ItemAttachmentSave($aFolder, $oOL_Item, Default, 1, "D:\attachment\")

should be

_OL_ItemAttachmentSave($oApp, $oOL_Item, Default, 1, "D:\attachment\")

BTW:

  • There is no need to add Sleep statements to your code. They just increase processing time.
  • You shouldn't do so much processing in your event handler.
    • You have so many Sleep statements that it takes more than 13 seconds to process a mail.
    • You delete files in the event handler an the main script
    • Why do you kill IE? I do not see any need
    • Setting Global variables in the event handler (like $Sendername, $Subject) does not make sense. Define them in the main 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

Did you modify your script as I described in my previous post?

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

The current code looks like as below:-

Iam just trying to keep the script run for loop to check all the email's that comes to the inbox folder for an event to trigger the rest of the functions...

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StringConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>
#include <String.au3>
#include <UIAWrappers.au3>
#include <GuiEdit.au3>
#include <Clipboard.au3>
#include <WinAPI.au3>
#include <ColorConstants.au3>
#include <FontConstants.au3>
#include <ProgressConstants.au3>
#include <Restart.au3>
#include <_IEquerySelectorAll.au3>
#include <GuiRichEdit.au3>
#include <hiddenDesktopInteract.au3>
#include <Date.au3>
#include <CUIAutomation2.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <OutlookEX.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

;Global $oErrorHandler = ObjEvent("Autoit.Error", "ComErrorHandler")

FileDelete("D:\attachment\*.*")
FileDelete("D:\Email-Details\*.*")
FileDelete("D:\Script-temp\*.*")
FileDelete("D:\Downloaded-Cert\*.*")
FileDelete("D:\LOG\*.*")
RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)

;Global $Errorcomments
;If Not IsDeclared($Errorcomments) Then Global $Errorcomments

; *****************************************************************************
; Example Script
; Handle Outlook NewmailEX event when a new mail arrives.
; This script loops until Shift-Alt-E is pressed to exit.
; *****************************************************************************
HotKeySet("+!e", "_Exit") ;Shift-Alt-E to Exit the script
;MsgBox(64, "OutlookEX UDF Example Script", "Hotkey to exit the script: 'Shift-Alt-E'!")



Global $oOApp = ObjCreate("Outlook.Application")
Global $test = ObjEvent($oOApp, "oOApp_")

While 1
    Sleep(400)
WEnd

FileDelete("D:\attachment\*.*")
FileDelete("D:\Email-Details\*.*")
FileDelete("D:\Script-temp\*.*")
FileDelete("D:\Downloaded-Cert\*.*")
FileDelete("D:\LOG\*.*")
RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)

; Outlook 2007 - NewMailEx event - http://msdn.microsoft.com/en-us/library/bb147646%28v=office.12%29.aspx
Func oOApp_NewMailEx($sOL_EntryId)
    Global $sMailbox = "certauto@xyz.com\Inbox"
    FileDelete("D:\attachment\*.*")
    FileDelete("D:\Email-Details\*.*")
    FileDelete("D:\Script-temp\*.*")
    FileDelete("D:\Downloaded-Cert\*.*")
    FileDelete("D:\LOG\*.*")
    RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)
    Sleep(1000)
    Global $aFolder = _OL_FolderAccess($oOApp, $sMailbox)
    ;If @error Then Exit MsgBox($MB_IconError, $sTitle, "Error when calling _OL_FolderAccess: @error=" & @error & ", @extended=" & @extended & @CRLF)

    Local $oOL_Item = $oOApp.Session.GetItemFromID($sOL_EntryId, Default)
    Sleep(1000)
    _OL_ErrorNotify(2)
    _OL_ItemAttachmentSave($oOApp, $oOL_Item, Default, 1, "D:\attachment\")
    Sleep(1000)
    ;MsgBox(64, "OutlookEX UDF Example Script", "New mail has arrived!" & @CRLF & @CRLF & _
    ;"From:    " & $oOL_Item.SenderName & @CRLF & _
    ;"Subject: " & $oOL_Item.Subject)
    Local $oOL_Item1 = $aFolder.Session.GetItemFromID($sOL_EntryId, Default)
    Sleep(1000)
    Local $oOL_Sender = $oOL_Item1.Sender

    If $oOL_Sender.AddressEntryUserType = $olExchangeUserAddressEntry Or $oOL_Sender.AddressEntryUserType = $olExchangeRemoteUserAddressEntry Then
        Local $oExchangeUser = $oOL_Sender.GetExchangeUser
    Else
        Sleep(2000)
    EndIf




    Sleep(1000)

    Local $sendername = ("D:\Email-Details\sendername.txt")
    Local $Subject = ("D:\Email-Details\subject.txt")

    ;=================================Subjectfile file write ===========================================
    Local $Subject1 = FileOpen($Subject, $FO_APPEND)
    If $Subject1 = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the Subject file.")
        Return False
    EndIf

    ; Write data to the file using the handle returned by FileOpen.
    FileWrite($Subject1, $oOL_Item1.Subject)

    ; Close the handle returned by FileOpen.
    FileClose($Subject1)
    Sleep(1000)


    ;=================================sendername file write ===========================================



    _ReplaceStringInFile($Subject, "Certifcate Request - ", "")
    _ReplaceStringInFile($Subject, " -- ", @CRLF)

    Local $hFileOpen = FileOpen($Subject, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")

    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen, 2)
    FileWrite($sendername, $sFileRead)
    _ReplaceStringInFile($Subject, $sFileRead, "")
    FileClose($hFileOpen)
    Sleep(2000)
    ;=================================End sendername file write ===========================================
    $Filerunpath = ("D:\NOC script\Test.Exe")
    Local $iPID = Run($Filerunpath)
    ProcessWaitClose($iPID)
    Sleep(1000)

    _OL_ItemModify($oOApp, $oOL_Item, Default, "Unread=False")
    ;$openemail = WinActivate ( "[CLASS:NetUIHWND; INSTANCE:1]", "" )


    ;ControlClick($openemail, "", "NUIDialog")
    ;$oOApp.Session.Logoff
    Sleep(1000)


    ;====================Mark email as read===========================================


    ;_OL_ItemModify($oOL_Item,$oOApp.Session.GetItemFromID($sOL_EntryId, Default, "Unread=False")


    Sleep(1000)


EndFunc   ;==>oOApp_NewMailEx


Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

Edited by Cyborg5000
Link to comment
Share on other sites

This was the original code before any modifications:-

(Had modified it, thought the script is trying to check each new email's from all the folders instead of the inbox, though some filters are set to sort out the emails and only the emails with particular subject line and body should be in the inbox)

 

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StringConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>
#include <String.au3>
#include <UIAWrappers.au3>
#include <GuiEdit.au3>
#include <Clipboard.au3>
#include <WinAPI.au3>
#include <ColorConstants.au3>
#include <FontConstants.au3>
#include <ProgressConstants.au3>
#include <Restart.au3>
#include <_IEquerySelectorAll.au3>
#include <GuiRichEdit.au3>
#include <hiddenDesktopInteract.au3>
#include <Date.au3>
#include <CUIAutomation2.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <OutlookEX.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

;Global $oErrorHandler = ObjEvent("Autoit.Error", "ComErrorHandler")

FileDelete("D:\attachment\*.*")
FileDelete("D:\Email-Details\*.*")
FileDelete("D:\Script-temp\*.*")
FileDelete("D:\Downloaded-Cert\*.*")
FileDelete ("D:\LOG\*.*")
RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)

;Global $Errorcomments
;If Not IsDeclared($Errorcomments) Then Global $Errorcomments

; *****************************************************************************
; Example Script
; Handle Outlook NewmailEX event when a new mail arrives.
; This script loops until Shift-Alt-E is pressed to exit.
; *****************************************************************************
HotKeySet("+!e", "_Exit") ;Shift-Alt-E to Exit the script
;MsgBox(64, "OutlookEX UDF Example Script", "Hotkey to exit the script: 'Shift-Alt-E'!")



Global $oOApp = ObjCreate("Outlook.Application")
Global $test = ObjEvent($oOApp, "oOApp_")

While 1
    Sleep(400)
WEnd

FileDelete("D:\attachment\*.*")
FileDelete("D:\Email-Details\*.*")
FileDelete("D:\Script-temp\*.*")
FileDelete("D:\Downloaded-Cert\*.*")
FileDelete ("D:\LOG\*.*")
RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)

; Outlook 2007 - NewMailEx event - http://msdn.microsoft.com/en-us/library/bb147646%28v=office.12%29.aspx
Func oOApp_NewMailEx($sOL_EntryId)

    FileDelete("D:\attachment\*.*")
    FileDelete("D:\Email-Details\*.*")
    FileDelete("D:\Script-temp\*.*")
    FileDelete("D:\Downloaded-Cert\*.*")
FileDelete ("D:\LOG\*.*")
    RunWait("taskkill /F /IM iexplore.exe /T", "", @SW_HIDE)
    Sleep(1000)
    Local $oOL_Item = $oOApp.Session.GetItemFromID($sOL_EntryId, Default)
    Sleep(1000)
    _OL_ItemAttachmentSave($oOApp, $oOL_Item, Default, 1, "D:\attachment\")
    Sleep(1000)
    ;MsgBox(64, "OutlookEX UDF Example Script", "New mail has arrived!" & @CRLF & @CRLF & _
    ;"From:    " & $oOL_Item.SenderName & @CRLF & _
    ;"Subject: " & $oOL_Item.Subject)
    Local $oOL_Item1 = $oOApp.Session.GetItemFromID($sOL_EntryId, Default)
    Sleep(1000)
    Local $oOL_Sender = $oOL_Item1.Sender

    If $oOL_Sender.AddressEntryUserType = $olExchangeUserAddressEntry Or $oOL_Sender.AddressEntryUserType = $olExchangeRemoteUserAddressEntry Then
        Local $oExchangeUser = $oOL_Sender.GetExchangeUser
    else
        Sleep(2000)
    EndIf



    ;=================================sendername file write ===========================================
    Local $sendername = ("D:\Email-Details\sendername.txt")
    Local $Subject = ("D:\Email-Details\subject.txt")


_ReplaceStringInFile($Subject, "Certifcate Request - ", "")
_ReplaceStringInFile($Subject, " -- ", @CRLF)

Local $hFileOpen = FileOpen($Subject, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")

    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen, 2)
    FileWrite ($sendername, $sFileRead)
    _ReplaceStringInFile($Subject, $sFileRead, "")


    ;=================================End sendername file write ===========================================
    Sleep(1000)

    ;=================================Subjectfile file write ===========================================
 Local $Subject1 = FileOpen($Subject, $FO_APPEND)
    If $Subject1 = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the Subject file.")
        Return False
    EndIf

    ; Write data to the file using the handle returned by FileOpen.
   FileWrite($Subject1, $oOL_Item1.Subject)

    ; Close the handle returned by FileOpen.
    FileClose($Subject1)
    Sleep(1000)


    ;=================================END Subjectfile file write ===========================================

    ;====================Mark email as read===========================================


    ;_OL_ItemModify($oOL_Item,$oOApp.Session.GetItemFromID($sOL_EntryId, Default, "Unread=False")


    Sleep(1000)


EndFunc   ;==>oOApp_NewMailEx


Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

Edited by Cyborg5000
Link to comment
Share on other sites

Seems you are running your script on a quite old version of Windows server (2012_R2).
Which version of Outlook do you run?

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

41 minutes ago, water said:

Seems you are running your script on a quite old version of Windows server (2012_R2).
Which version of Outlook do you run?

MS office/outlook 2010..

My MS 365 had 5 license's which were consumed....and since it keeps updating online, thought of choosing an old version.

Link to comment
Share on other sites

MS has changed the way events get triggered.
In older versions of Outlook multiple mails could trigger a single event. Never versions seem to trigger an event for every mail.

Could you please test the following script? It should work for old an new Outlook versions.

#include <OutlookEX.au3>

; *****************************************************************************
; Example Script
; Handle Outlook NewmailEX event when one/multiple new mail(s) arrive(s) in your Inbox.
; This script loops until Shift-Alt-E is pressed to exit.
; *****************************************************************************
HotKeySet("+!e", "_Exit") ;Shift-Alt-E to Exit the script
MsgBox(64, "OutlookEX UDF Example Script", "Hotkey to exit the script: 'Shift-Alt-E'!")

Global $oOL = _OL_Open()
Global $oEvent = ObjEvent($oOL, "oOApp_")

While 1
    Sleep(10)
WEnd

; NewMailEx event - https://docs.microsoft.com/en-us/office/vba/api/outlook.application.newmailex
Func oOApp_NewMailEx($sEntryIDs)

    Local $iItemCount, $oItem, $aEntryIDs = StringSplit($sEntryIDs, ",", $STR_NOCOUNT)
    $iItemCount = UBound($aEntryIDs)
    ConsoleWrite("OutlookEX UDF Example Script - " & ($iItemCount = 1 ? "new item has" : "new items have") & " arrived!" & @CRLF & @CRLF)
    For $i = 0 To $iItemCount - 1
        $oItem = $oOL.Session.GetItemFromID($aEntryIDs[$i], Default)
        ConsoleWrite( _
                "From:    " & $oItem.SenderName & @CRLF & _
                "Subject: " & $oItem.Subject & @CRLF & _
                "Class:   " & $oItem.Class & " (43=Mail, 53=MeetingRequest ...)" & @CRLF)
    Next
EndFunc   ;==>oOApp_NewMailEx

Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

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

Hello @water

Greetings,

is this error expected if no attachment is captured or even if no email reaches the mailbox?

 

"C:\Program Files (x86)\AutoIt3\Include\OutlookEX.au3" (2080) : ==> The requested action with this object has failed.:
If $vItem.Attachments.Count = 0 Then Return SetError(9, 0, 0)
If $vItem^ ERROR
->03:12:33 AutoIt3.exe ended.rc:1
+>03:12:33 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 93.83


 

Edited by Cyborg5000
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...