Question

DC787 on Tue, 05 Sep 2017 05:27:41


Hi I was testing some parts of a code and found something strange on Explorer Close event.

The code is taken from the book “Professional Outlook 2007 Programming”:

' Monitor Explorer_Close to see when UI "disappears"

    Private Sub m_olExplorer_Close() Handles m_olExplorer.Close

        'release current reference

        m_olExplorer = Nothing

 

        Try

            m_olExplorer = m_objOutlook.ActiveExplorer           

        Catch ex As Exception

            Err.Clear()

            If m_objOutlook.Inspectors.Count = 0 Then

                'release add-in objects

                If m_blnTeardown = False Then

                    Call TearDown()

                End If

            End If

        End Try

    End Sub

 

My Code(When you are closing the last explorer m_objOutlook.ActiveExplorer returns nothing and no error is thrown. Hence the code is shifted to the Finally section of the try catch):

' Monitor Explorer_Close to see when UI "disappears"

    Private Sub m_olExplorer_Close() Handles m_olExplorer.Close

        'release current reference

        m_olExplorer = Nothing

 

        Try

            m_olExplorer = m_objOutlook.ActiveExplorer           

        Catch ex As Exception

            Err.Clear()

        Finally

            If m_olExplorer Is Nothing And m_objOutlook.Inspectors.Count = 0 Then

                'release add-in objects

                If m_blnTeardown = False Then                   

                    Call TearDown()

                End If

            End If

        End Try

    End Sub

 

When I have 1 explorer, the close event works fine.

However, when I have more than 1 Explorers then the code m_objOutlook.ActiveExplorer returns mixed results on m_olExplorer_Close event:

     In Outlook 2013 m_objOutlook.ActiveExplorer returns the explorer that is currently being closed. I verified this by looking at the caption of the Explorer. When I see some of the properties of m_objOutlook.ActiveExplorer object such as m_objOutlook.ActiveExplorer.Currentfolder the error message is shown as shown:


-       Same test was done in Outlook 2007 and m_objOutlook.ActiveExplorer returns the correct active explorer.

I don’t understand why this is happening.

As a result of this causes m_olExplorer_Close event to only fire once in Outlook 2013 even though I try to close multiple explorers. However in Outlook 2007, m_olExplorer_Close event fires for each explorer close.








Sponsored



Replies

Dmitry Streblechenko _MVP_ on Wed, 06 Sep 2017 03:23:16


I am not sure why you'd want to use m_objOutlook.ActiveExplorer if you already have m_olExplorer. Is m_olExplorer a pointer to a single instance of an Explorer class? What you need to do is have a class that wraps the Explorer object as its member variable and has explorer event handlers as its methods. You will create a new instance of that class on startup for Application.ActiveExplorer (if not null) and then  why Explorers.NewExplorer event fires. This way when Explorer.Cose event fires, you will know which Explorer object it refers to since there is only one Explorer object for that wrapper class.

DC787 on Wed, 06 Sep 2017 03:35:21


Thanks Dmitry.

In that case, where would I have to call the TearDown code for a COM Addin?

Dmitry Streblechenko _MVP_ on Wed, 06 Sep 2017 03:39:58


Your Explorer_Close event on the class wrapping the Explorer object can have a back pointer to the addin class that contains the list of Explorer class wrappers. If the number of items in that list is 1, that means this is the last explorer being closed (keep in mind you can also have open inspectors). Otherwise just remove the Explorer wrapper class from that list.

DC787 on Wed, 06 Sep 2017 03:53:24


Thanks Dmitry.

I will try that out and let you know.

I am still puzzled by 1 thing, if there are multiple explorers and you close 1 of the explorers why do I get mixed results in m_olExplorer_Close for m_objOutlook.ActiveExplorer, in Outlook 2007 when compared to Outlook 2013.

Did some thing change in Outlook 2013? 

Dmitry Streblechenko _MVP_ on Wed, 06 Sep 2017 04:14:46


Because what constitutes the "active explorer" is really undefined: is it the explorer being closed? Or the one left open?

DC787 on Wed, 06 Sep 2017 04:37:09


Both in Outlook 2007 and in Outlook 2013 the explorer is closed and not visible on the UI when the m_olExplorer_Close event is triggered.

My test case: Open Outlook -> select the Sent Items folder -> right click and "Open in New Window". Now I have 2 explorers 1 with the Inbox as the current folder and the other explorer with the Sent Folder as the current folder.

When I close the explorer with Sent Folder as the current folder the following happens in the m_olExplorer_Close event:

- in Outlook 2007: m_objOutlook.ActiveExplorer.Caption returns Explorer with Inbox as the current folder(default inbox folder selected) and I am able to access all properties e.g. the current folder object(m_objOutlook.ActiveExplorer.CurrentFolder)

- in Outlook 2013: m_objOutlook.ActiveExplorer.Caption returns with Sent Folder as the current folder and accessing the properties returns an error e.g. the current folder object(m_objOutlook.ActiveExplorer.CurrentFolder) returns:



Ken Slovak MVP on Wed, 06 Sep 2017 15:21:48


It's a matter of when exactly the ActiveExplorer goes out of scope. Things changed in later versions of Outlook. However, a plus is that now the Application.Quit event fires when Outlook is closing, so that can now be used. A minus is that the Extensibility OnShutdown event no longer fires.

As Dmitry mentioned you need to check the Explorers.Count to know if the Explorer.Close event is prompting further action.

You may also have to account for "headless" Outlook instances, where no Explorer was ever opened, for example cases where Outlook is started by a Simple MAPI Send To operation.

DC787 on Thu, 07 Sep 2017 06:28:15


Thank you for all your answers, it was really helpful.