Outlook Close Loop - Macro Development

Posted by GeorgeH on January 26, 2011

I caused myself some troubles when developing a Macro for Microsoft Outlook 2007, where part of the code was to close the active Inspector after completing a series of operations on the current item.



A macro running from Outlook registers a new Inspector as it opens as a public MailItem object with event handlers. A close event is included in the Macro to capture when a message is closed (after being read or sent). The public mailitem object is set from the item loading event sub.



Problem: When attempting to close the inspector if certain criteria was met during processing, by mistake I used the Application.ActiveExplorer object instead of the Application.ActiveInspector. The result, was that Outlook itself closed when the event fired. If the event was fired by loading an item without opening in a separate Inspector, the criteria required for the form to close was always satisfied. The result was that Outlook closed if I tried to open any item, as it attempted to load a preview of the message in the reading pane before loading the Inspector.



When I attempted to restart Outlook, the Loading event fired which resulted in Outlook closing straight away with an "Unexpected Error has occurred" message. After a second attempt, Outlook detected that there were issues with an addin, and gave me the option to disable the addin. I didn't disable the addin.



My solution was to adjust the Outlook Macro Security settings via the registry (Regedit). The original security configuration was set to allow any macro to run.



In the Windows Registry, I found the record in the following class:



HKEY_CURRENT_USERSoftwareMicrosoftOffice.0OutlookSecurity



The initial ("No Security Check for Macros") security setting was configured in the "Level" key within the above location. This was represented by a "1" as the value of the key. I changed the value in the key to "2", which represents the "Warnings for all Macros" security option.



I then opened Outlook, which prompted for me to enable or disable Macros. I disabled Macros, opened the Visual Basic Editor and fixed the problem in the code that caused Outlook to close after opening. The initial security setting could then be re-applied either using the Registry editor or directly from Outlook.



Microsoft Outlook: Calling VBA Class Module function using VBScript



Programming tutorials and code snippets

Rating

4/5

Reviews

There are currently no comments or reviews.

Submit a review:

Login required.