Jump to content

Outlook, COM and events


Recommended Posts

Hi all

First post to the forum!

I'm trying to handle events generated by Outlook. Specifically, I'm interested in those events associated with the currently running copy of Outlook and specifically the 'Application' object. In the code below I am trying to get notified when the NewMail event fires, however I don't :).

I have tried specifying the 3rd optional parameter to objevent (e.g. "Application", "_Application", "ApplicationEvents" etc) but to no avail...when run the $oEvt variable is not set and hence I get the popup.

I'm running Autoit Beta 3.1.1.121. Environment is W2K SP4, Outlook 2003 SP1.

Thanks in advance for any help,

-b

ObjEvent ( "AutoIt.Error" , "MyErrFunc" )

$outlook = ObjCreate("Outlook.Application")
$activeexplorer = $outlook.ActiveExplorer; this is included for debug reasons

ConsoleWrite("Version is " & $outlookapp.Version & @CR)
if @error = 1 Then
    ConsoleWrite("Error getting obj ref to Outlook" & @CR)
    Exit
Else
    ConsoleWrite("Found outlook..." & @CR)
    
EndIf

$oEvt = ObjEvent($outlook, "OutlookEvent_"); there are various event interfaces (Application, Explorer etc)

If Not IsDeclared ("oEvt") then
    MsgBox(0,"", "$event is NOT declared")
    Exit
EndIf

while 1
    ConsoleWrite("Sleeping " & @CR)
    ConsoleWrite("Width of active explorer window is " & $activeexplorer.Width & @CR); debug: proving our reference is to the visible running copy.  Resizing the window with the mouse results in this property changing as expected
    sleep(10000)
WEnd

func OutlookEvent_($name)
    ConsoleWrite("Event catchall for " & $name & @CR)
EndFunc

Func MyErrFunc($oMyError)
    ConsoleWrite("COM error" & @CR)
    $HexNumber=hex($oMyError.number,8)   ; for displaying purposes
    ConsoleWrite("COM Error: err.description is: " & @TAB & $oMyError.description & " err.number is: " & @TAB & $HexNumber & " err.scriptline is: " & @TAB & $oMyError.scriptline & @CR)
    SetError(1) ; to check for after this function returns
EndFunc
Link to comment
Share on other sites

OK, no takers fo far :/

To troubleshoot this further, I installed OutlookSpy which puts a handy toolbar into outlook and lets you explore the outlook object model. Using this I can see the newmail event fire as new mail arrives so pretty confident my outlook install isn't the culprit.

The "Application object" for Outlook is known as '_Application' and I've tried adding that as the 3rd parameter to the objevent call but no dice.

I've also experimented with events in other outlook objects (e.g. a mail item). I'm able to receive those fine, my event handlers fire as email messages are closed etc. I do this through creating a Application.ActiveInspector.CurrentItem object and setting up events as per usual.

Any ideas how I can troubleshoot thisfurther? Without source to Autoit, I'm striggling to "see" what's going on; i.e. why the autoit script is not receiving the events from the Application object. Any tips? Any debug builds of beta's that could provide useful info?

Struggling...

-b

When I say 'source to Autoit', I should say 'Source to latest beta'. Not chasing for source as I understand its with the trusted few, after some pointers, e.g. calls to trap in windbg etc from those in the know...

Edited by balcra
Link to comment
Share on other sites

To be honest, I looked at this briefly when you first posted, saw typos in the first few lines and decided I'd have to come back to it...

First line you need $oMyError = ObjEvent...

Then you define $outlook and later refer to $outlookapp.

To make debugging a little faster I decided to include IE.au3 (see my sig for IE.au3 T2.0) and use the errorhandler code I have defined there, broke it down to these few lines:

#include <IE.au3>
_IEErrorHandlerRegister()
$outlook = ObjCreate("Outlook.Application")
$oEvt = ObjEvent($outlook, "OutlookEvent_")

and received this error:

>Running:(3.1.1.125):C:\Program Files\AutoIt3\beta\autoit3.exe "C:\Documents and Settings\hohm\My Documents\My Code\tmp-kljhert.au3"    
--> COM Error Encountered in tmp-kljhert.au3
----> $IEComErrorScriptline = 4
----> $IEComErrorNumberHex = 00000007
----> $IEComErrorNumber contains full error number
----> $IEComErrorWinDescription = Failed to retrieve outgoing Event Interface from Object.
----> $IEComErrorDescription = 0
----> $IEComErrorSource = 0
----> $IEComErrorHelpFile = 0
----> $IEComErrorHelpContext = 0
----> $IEComErrorLastDllError = 0

So your event handler is not getting defined. I have not seen this error before and a quick google search found nothing. Perhaps you can find something in MSDN.

When I face trouble like this, one of the first things I do is attempt to replicate in VBScript. That way you'll know if you are chasing something specific to AutoIt or if it is more generalized.

Dale

p.s. there is also a problem with your IsDeclared check I suggest IsObj($oEvt)

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Thanks for the suggestions Dale - most helpful. When I made the necessary fixes to the submitted script, I got exactely the same result you did.

According to the MS docs on the the NewMail/NewMailEx events, these are not accessible to VBScript. I've therefore had to go round the houses a bit and take a stab at a C# (first time for everything). I went ahead and installed Visual C# Express and and found a MS article with a code sample for handling NewMailEx events. Tweaked this a tiny amount, built it and tested. I successfully received events. Here's the code I used:

using System;
using System.Text;
using OutLookApp = Microsoft.Office.Interop.Outlook;

namespace OPine
{
    class OPineApp
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Create an Outlook application object. 
            OutLookApp.ApplicationClass objOutlook = new OutLookApp.ApplicationClass();

            // Ring up the new message event.
            //OutLookApp.ApplicationEvents_11_NewMailExEventHandler
            objOutlook.NewMailEx += new OutLookApp.ApplicationEvents_11_NewMailExEventHandler(OutLookApp_NewMailEx);
            Console.WriteLine("Please wait for new messages...");
            Console.ReadLine();
        }
        #region NewMail event handler.
        private static void OutLookApp_NewMailEx(string EntryIDCollection)
        {
            Console.WriteLine("You've got a new mail whose EntryIDCollection is " + EntryIDCollection);
        }
        #endregion
    }

}

It was necessary to add a reference to the Microsoft.Office.Interop.Outlook COM object before building.

Anyway, this does appear to show that the events fire and can be received OK.

Is there anything else I should do testwise or would the evidence suggest this is an issue with Autoit? Either way, I'm keen to get this resolved so let me know what I can do.

Thanks again,

-b

P.S I've attached the compiled exe for the above

[EDIT] OK, I lied - the file attachment didn't seem to work for me, feel free to PM and I'll email it over

Edited by balcra
Link to comment
Share on other sites

If it won't work in VBScript it is almost certain not to work with the COM interface in AutoIt.

Ready the background on the COM interface in AutoIt in the helpfile to see some of the limitations -- in particular, the requirement for the iDispatch interface. This is likely why it can't be exposed in VBScript as well.

Best you'd likely be able to do is to create a dll in something like C# and then access that with AutoIt rather than using COM.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

  • 4 years later...

Just for the records.

Running the following script with Outlook 2010 gets you a MsgBox for every mail you receive!

#AutoIt3Wrapper_UseX64=n
#include <IE.au3>
_IEErrorHandlerRegister()
$outlook = ObjCreate("Outlook.Application")
$oEvt = ObjEvent($outlook, "OutlookEvent_")
While 1
    Sleep(100)
WEnd
Func OutlookEvent_NewMailEx()
    MsgBox(0,"","NewMailEx event triggered!")
EndFunc

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...