Jump to content

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


Recommended Posts

When you open/connect to Outlook this way

Global $oOApp = ObjCreate("Outlook.Application")

then there is no COM error handler.
Either use _OL_Open or create a COM error handler yourself.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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,

 

I tried modifying the code as you suggested, but autoit crashes....

>Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "D:\script\emailnotification.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
!>01:13:38 AutoIt3.exe ended.rc:-1073741819
+>01:13:38 AutoIt3Wrapper Finished.
>Exit code: 3221225477    Time: 782.9

 

#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_")

Global $oOApp = _OL_Open()
Global $test = ObjEvent($oOApp, "oOApp_")

While 1
    Sleep(1000)
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)
    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_Item.Sender

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

    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_Item.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\CSRSITEVALIDATE.Exe")
    Local $iPID = Run($Filerunpath)
    ProcessWaitClose($iPID)
    sleep (1000)

    $Filerunpath = ("D:\NOC script\SCRIPTCSRVALIDATE.Exe")
    Local $iPID = Run($Filerunpath)
    ProcessWaitClose($iPID)
    sleep (1000)

    $Filerunpath = ("D:\NOC script\Cancelledticket.Exe")
    Local $iPID = Run($Filerunpath)
    ProcessWaitClose($iPID)
    sleep (1000)

    $Filerunpath = ("D:\NOC script\Resolveticket.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

 

Link to comment
Share on other sites

Please add

_OL_ErrorNotify(2)

after _OL_Open. So we can see if there is a COM error before the crash.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

Just a bit background of the mailbox that iam using.

The mailbox is a service account.

It receives also emails that don't have attachments, however i have set filters for such emails that only an email with specific subject line should be moved or kept in the Inbox folder and rest are deleted.

The email's that are moved or kept in the In-box folder are the main emails that iam trying to get details from, so that the rest of the scripts can move on, only with the attachment and subject details that are grabbed by this event.

Just assuming that you should know this in case these failures are related to it.....

Could be the failure due to reading of the emails before they come to the inbox folder since its an event?

 

Also modified my code as per an example in the ZIP

At a new email notification the script works fine, however it again starts starting other call outs/run that i have mentioned and errors out.

 

#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_")

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($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 = $oOApp.Session.GetItemFromID($aEntryIDs[$i], Default)
        _OL_ItemAttachmentSave($oOApp, $oItem, Default, 1, "D:\attachment\")
        Sleep(1000)
        Local $oOL_Sender = $oItem.Sender
        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, $oItem.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\CSRSITEVALIDATE.Exe")
        Local $iPID = Run($Filerunpath)
        ProcessWaitClose($iPID)
        Sleep(1000)

        $Filerunpath = ("D:\NOC script\SCRIPTCSRVALIDATE.Exe")
        Local $iPID = Run($Filerunpath)
        ProcessWaitClose($iPID)
        Sleep(1000)

        $Filerunpath = ("D:\NOC script\Cancelledticket.Exe")
        Local $iPID = Run($Filerunpath)
        ProcessWaitClose($iPID)
        Sleep(1000)

        $Filerunpath = ("D:\NOC script\Resolveticket.Exe")
        Local $iPID = Run($Filerunpath)
        ProcessWaitClose($iPID)
        Sleep(1000)


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

        ;ControlClick($openemail, "", "NUIDialog")
        ;$oOApp.Session.Logoff
        Sleep(1000)
    Next
EndFunc   ;==>oOApp_NewMailEx



Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

Edited by Cyborg5000
Link to comment
Share on other sites

  • 3 weeks later...

Hello @water

Greetings, !😄

Hope you are doing well!

Need some guidance. what can i do to make the above script to check only the folder under inbox "sdfsd@company.com\Cert".

Also if there are no attachments, then skip that email, but capture/work only the email which has an attachment.

(Asking this, as the mailbox that iam using is of a service account, where i all emails come in, and the automation that iam working onto is having a specific subject line and has attachment)

I have set rules to move only the particular subject line email should go to the cert folder and rest are set to auto delete.

Ref scope that i have tried so far:-

Global $aFolder = _OL_FolderAccess($oOApp, "certauto@xyz.com\certs")
    Local $iItemCount, $aFolder, $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 = $oOApp.Session.GetItemFromID($aEntryIDs[$i], Default)

        $aAttachments = _OL_ItemAttachmentGet($oOApp, $oItem, $aFolder)
        If @error = 3 Then ContinueLoop ; Mail without attachments
        For $j = 1 To $aAttachments[0][0]
            _OL_ItemAttachmentSave($oOApp, $oItem, Default, $j, "D:\attachment\")
            Sleep(1000)
            Local $oOL_Sender = $oItem.Sender
            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, $oItem.Subject)

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

 

Edited by Cyborg5000
Link to comment
Share on other sites

The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs.
... However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously.
(https://docs.microsoft.com/en-nz/office/vba/api/outlook.application.newmailex)

So your function oOApp_NewMailEx will process EVERY incoming mail. If one of your rules jumps in and moves the item to another folder your script might crash as the item you want to process no longer exists at the place you expect.

I suggest to split processing into two steps:

  • process all new mail items that arrive and remain in the inbox
  • set the ItemAdd event to the subfolder you want to process

This means: Process the mail items in the folder where they are intended to stay.

BTW: Please remove ALL sleep statements. They are of no use. Taken from the above link: "Use this method with caution to minimize the impact on Outlook performance."

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

34 minutes ago, water said:

The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs.
... However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously.
(https://docs.microsoft.com/en-nz/office/vba/api/outlook.application.newmailex)

So your function oOApp_NewMailEx will process EVERY incoming mail. If one of your rules jumps in and moves the item to another folder your script might crash as the item you want to process no longer exists at the place you expect.

I suggest to split processing into two steps:

  • process all new mail items that arrive and remain in the inbox
  • set the ItemAdd event to the subfolder you want to process

This means: Process the mail items in the folder where they are intended to stay.

BTW: Please remove ALL sleep statements. They are of no use. Taken from the above link: "Use this method with caution to minimize the impact on Outlook performance."

Thanks for your time @water and for helping.

What  can i do for emails which have no attachments to skip those emails, using the above format of the script and work only on the emails with attachments.

Link to comment
Share on other sites

Check the number of attachments.

If $oItem.Attachments.Count > 0 Then
    ; Process this mail
EndIf

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

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...