locked
Can't get Wordeditor property from the Inspector.Activate event in the Outlook07 RRS feed

  • Question

  •   Hi!

           

        I created an outlook-addin for the Outlook 2007, and I tried to get the word editor property from the Inspector.Activate event, it works well in the normal cases.

     

       But if I open a new outlook, then double click an email under the "Draft" or the "Inbox" folder, I found I can't get the Wordeditor property in the Inspector.Activate event!

     

      And I have searched in the MSDN, it just says "In Outlook 2007, the Inspector.WordEditor property does not work in the NewInspector event. To work around this issue, implement the Inspector.Activate event, and then use the Inspector.WordEditor property in the Inspector.Activate event. "

     

       Why we can't get the WordEditor property in the Inspector.Activate event in this case? Any work around? My outlook version is 12.0.4518.1014.

     

          TIA!!!
    Tuesday, April 10, 2007 10:01 AM

Answers

  • The documentation also states that:

    The WordEditor property is only valid if IsWordMail returns True and the EditorType is olEditorWord. The returned WordDocument object provides access to most of the Word object model except for the following members:

     

    Do you check those properties prior to getting the WordEditor?

    Wednesday, April 11, 2007 7:07 PM
  • This is weird. I am always able to get to the WordEditor.

     

    Here is the code that I have written:

    Code Snippet

    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            Outlook.Inspectors inspectors;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            }

            void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector insp)
            {
                Outlook.MailItem mailIteam = insp.CurrentItem as Outlook.MailItem;
                if (mailIteam != null)
                {
                    InspectorEvents.CreateInstance(insp);
                }
            }

            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }

            #region VSTO generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }

            #endregion
        }

        public class InspectorEvents
        {
            static System.Collections.ArrayList s_inspectorEventsCollection;

           Outlook.Inspector inspector;
            Outlook.InspectorEvents_10_Event events;

            static public InspectorEvents CreateInstance(Outlook.Inspector inspector)
            {
                return new InspectorEvents(inspector);
            }

            public InspectorEvents(Outlook.Inspector inspector)
            {
                if (s_inspectorEventsCollection == null)
                    s_inspectorEventsCollection = new System.Collections.ArrayList();

                events = inspector as Outlook.InspectorEvents_10_Event;
                if (events == null)
                    return;

               events.Activate += events_Activate;
                events.Close += events_Close;

                // add myself to inspector's events collection to prevent
                // this object from being garbage collected
                s_inspectorEventsCollection.Add(this);
            }

            void events_Activate()
            {
                object oDoc = this.inspector.WordEditor;
                if (oDoc != null)
                {
                    Word.Document doc = this.inspector.WordEditor as Word.Document;
                    if (doc != null)
                        MessageBox.Show("DocumentName is: " + doc.Name);
                }
            }

            void events_Close()
            {
                this.events.Activate -= events_Activate;
                this.events.Close -= events_Close;

                // now remove ourselves from the collection
                // to allow GC to collect us.
                s_inspectorEventsCollection.Remove(this);
            }
        }
    }

     

     

    Friday, April 13, 2007 5:54 PM

All replies

  • The documentation also states that:

    The WordEditor property is only valid if IsWordMail returns True and the EditorType is olEditorWord. The returned WordDocument object provides access to most of the Word object model except for the following members:

     

    Do you check those properties prior to getting the WordEditor?

    Wednesday, April 11, 2007 7:07 PM
  •     

     Thank you for your answer! So in the case which I described, is there any work around to get the Wordeditor?

     

     

    Thursday, April 12, 2007 3:03 AM
  • You did not answer my questions Smile
    Thursday, April 12, 2007 4:45 AM
  • Yes, I didn't check the IsWordMail porperty prior to getting the WordEditor.  Is there any work around to get the Wordeditor?
    Friday, April 13, 2007 2:51 AM
  • This is weird. I am always able to get to the WordEditor.

     

    Here is the code that I have written:

    Code Snippet

    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            Outlook.Inspectors inspectors;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            }

            void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector insp)
            {
                Outlook.MailItem mailIteam = insp.CurrentItem as Outlook.MailItem;
                if (mailIteam != null)
                {
                    InspectorEvents.CreateInstance(insp);
                }
            }

            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }

            #region VSTO generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }

            #endregion
        }

        public class InspectorEvents
        {
            static System.Collections.ArrayList s_inspectorEventsCollection;

           Outlook.Inspector inspector;
            Outlook.InspectorEvents_10_Event events;

            static public InspectorEvents CreateInstance(Outlook.Inspector inspector)
            {
                return new InspectorEvents(inspector);
            }

            public InspectorEvents(Outlook.Inspector inspector)
            {
                if (s_inspectorEventsCollection == null)
                    s_inspectorEventsCollection = new System.Collections.ArrayList();

                events = inspector as Outlook.InspectorEvents_10_Event;
                if (events == null)
                    return;

               events.Activate += events_Activate;
                events.Close += events_Close;

                // add myself to inspector's events collection to prevent
                // this object from being garbage collected
                s_inspectorEventsCollection.Add(this);
            }

            void events_Activate()
            {
                object oDoc = this.inspector.WordEditor;
                if (oDoc != null)
                {
                    Word.Document doc = this.inspector.WordEditor as Word.Document;
                    if (doc != null)
                        MessageBox.Show("DocumentName is: " + doc.Name);
                }
            }

            void events_Close()
            {
                this.events.Activate -= events_Activate;
                this.events.Close -= events_Close;

                // now remove ourselves from the collection
                // to allow GC to collect us.
                s_inspectorEventsCollection.Remove(this);
            }
        }
    }

     

     

    Friday, April 13, 2007 5:54 PM
  •       Thank you for your patience!

     

           Did you try the case I described: launch a new outlook07 application, then double click an email under the "Inbox" or the "Draft" folders, can you get the wordeditor?  In my codes, the IsWordMail is false.

    Saturday, April 14, 2007 7:13 AM
  • Yes, I tried opening messages from both Inbox and Draft folders.

    I was just reading Reading and Editing E-mail in Microsoft® Office Outlook® 2007  and it sounds like if you do not have Word 2007 installed - you might not be able to use Word's functionality.

    Can you check whether this is the case for you?

    Saturday, April 14, 2007 5:46 PM
  • Misha is there a VB.Net example of this. I have tried to convert this and failed...

    No matter what I do IsWordMail returns True and  EditorType is olEditorWord, but Wordeditor is always nothing.

    This is a VSTO Ribbon addin, Office Outlook 2007 and Word 2007, VS 2010 .

    Snippet:

    objOL = OutlookApplication.Application

    objInspector = objOL.Application.ActiveInspector

    objMsga = objOL.Application.ActiveInspector.CurrentItem

    strS = objInspector.EditorType 'just here to look at the values, always 4

    S1 = objInspector.IsWordMail 'just here to look at the values, always True

     

     

    If objInspector.EditorType = olEditorWord Then

    objInspector.Activate() 'Doesn't make any difference

    objDoc = objInspector.WordEditor 'always Nothing

     

     

    'objSel = objDoc.Windows(1).Selection

     

     

    End If

    objOL =

     

    Nothing

    objNS =

     

    Nothing

    Anything I try using the Event handlers produces the same result.

    Wednesday, December 15, 2010 3:00 PM
  • I'm not Misha, but maybe I can help with this 3 year old thread.
     
    In Outlook 2007 and 2010 you are always using WordMail, there is no Outlook editor. So IsWordMail is always True and EditorType will always be olEditorWord.
     
    This is correct and should return the document object:
     
        objDoc = objInspector.WordEditor
     
    I use code like that in both VB.NET and C# all the time. However, you need to wait until the first Inspector.Activate() event for that to be valid usually, before that you get only weak object references.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "Bill202" <=?utf-8?B?QmlsbDIwMg==?=> wrote in message news:b08769cc-b732-4b70-b4ec-50e41da8be5f...

    Misha is there a VB.Net example of this. I have tried to convert this and failed...

    No matter what I do IsWordMail returns True and  EditorType is olEditorWord, but Wordeditor is always nothing.

    This is a VSTO Ribbon addin, Office Outlook 2007 and Word 2007, VS 2010 .

    Snippet:

    objOL = OutlookApplication.Application

    objInspector = objOL.Application.ActiveInspector

    objMsga = objOL.Application.ActiveInspector.CurrentItem

    strS = objInspector.EditorType 'just here to look at the values, always 4

    S1 = objInspector.IsWordMail 'just here to look at the values, always True

     

     

    If objInspector.EditorType = olEditorWord Then

    objInspector.Activate() 'Doesn't make any difference

    objDoc = objInspector.WordEditor 'always Nothing

     

     

    'objSel = objDoc.Windows(1).Selection

     

     

    End If

    objOL =

     

    Nothing

    objNS =

     

    Nothing

    Anything I try using the Event handlers produces the same result.


    Ken Slovak MVP - Outlook
    Wednesday, December 15, 2010 4:00 PM
  • Hey Ken Thank you so much for taking the time!

    I put this in there: 

    "objInspector.Activate() 'Doesn't make any difference how long I wait.

    Perhaps this is machine/software specific. I am on a Win 7 64 bit.

    Also I have removed all the other Outlook Addins except for my masterpiece.

    BTW, it was desperation that lead to posting in a 3 year old thread. Over the past 3 days I have been all over MSDN and Google looking for an answer to this and have tried nearly every example I found. Everything I see says what I have should work, but it doesn't.

    Bill 

    Wednesday, December 15, 2010 4:23 PM
  • One more thing, you said you use this all the time. Do you use something akin to what Misha has posted?
    Wednesday, December 15, 2010 4:26 PM
  • I saw that with calling to Activate(). I usually handle the Activate() event and set things up there. I run code like that (mostly C#) on Win7 64-bit and even on Outlook 2010 64-bit with no problems, so I don't think it's an OS or bitness problem.
     
    My usual way of doing this would be in the Activate() event in a WMInspectorHandler wrapper class for WordMail items. Most of my samples are in C#, but it should be pretty easy to translate. The main piece setting up a Document object is only 1 line: _doc = (Word.Document)_insp.WordEditor;
     
    In that event handler my code would call the procedure to set up Word object references:
     

        InstantiateWordObjects();

    That procedure would be coded as follows:

    private void InstantiateWordObjects()

    {

    try

    {

    // _doc is a class level Word.Document object, _insp is a class level Outlook.Inspector object

    _doc = (Word.Document)_insp.WordEditor;

    if (_doc != null)

    {

    _docWindow = (Word.Window)_doc.ActiveWindow; // _docWindow == Word.Window

    _wordApp = (Word.Application)_doc.Parent; // _wordApp == Word.Application

    // Add the Word event handler, set CustomizationContext

    if (_wordApp != null)

    {

    try

    {

    object context = _wordApp.CustomizationContext;

    if (context == null)

    {

    _wordApp.CustomizationContext = _wordApp.NormalTemplate;

    }

    }

    catch (Exception ex1)

    {

    Logger.LogException(ex1, ex1.Message);

    }

    try

    {

    _oldCustomizationContext = _wordApp.CustomizationContext;

    }

    catch (Exception ex2)

    {

    Logger.LogException(ex2, ex2.Message);

    }

    //WindowActivate

    _wordApp.WindowActivate +=

    new Word.ApplicationEvents4_WindowActivateEventHandler(OnWindowActivate);

    }

    }

    }

    catch (System.Runtime.InteropServices.InvalidComObjectException comEX)

    {

    Logger.LogException(comEX, comEX.Message);

    }

    catch (Exception ex)

    {

    Logger.LogException(ex, ex.Message);

    }

    }

     


    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "Bill202" <=?utf-8?B?QmlsbDIwMg==?=> wrote in message news:cb284f33-e396-41d0-a3d0-2ddc13c0b515...

    Hey Ken Thank you so much for taking the time!

    I put this in there: 

    "objInspector.Activate() 'Doesn't make any difference how long I wait.

    Perhaps this is machine/software specific. I am on a Win 7 64 bit.

    Also I have removed all the other Outlook Addins except for my masterpiece.

    BTW, it was desperation that lead to posting in a 3 year old thread. Over the past 3 days I have been all over MSDN and Google looking for an answer to this and have tried nearly every example I found. Everything I see says what I have should work, but it doesn't.

    Bill 


    Ken Slovak MVP - Outlook
    Wednesday, December 15, 2010 5:48 PM
  • Sorry for the delay here but I had to get the rest of this done so I can get this one out the door, sans this little feature.

    I have been messing with this on and off for the past week and nothing works. I am wondering now if this could be something to do with Outllok setup. I have stopped all of the ancilary backround apps too.

    Here is what I am working with currently:

    Some of this is redundant, I know, just there for debugging.

    Imports

     

     

    Microsoft.Office.Interop

    Public

     

     

    Class ThisAddIn

     

     

    Public WithEvents _allInsp As Outlook.Inspectors

     

     

    Private WithEvents _thisInsp As Outlook.Inspector

     

     

    Private WithEvents _thisMail As Outlook.MailItem

     

     

     

    Protected Overrides Function CreateRibbonExtensibilityObject() As Office.IRibbonExtensibility

     

     

    Return Globals.Factory.GetRibbonFactory().CreateRibbonManager(New Microsoft.Office.Tools.Ribbon.IRibbonExtension() {New SiebelSave()})

     

     

    End Function

     

     

    Private Sub ThisAddIn_Startup() Handles Me.Startup

    _allInsp = Application.Inspectors

    objInspectors = _allInsp

     

     

    End Sub

     

     

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    objInspector =

     

    Nothing

    objInspectors =

     

    Nothing

    _thisInsp =

     

    Nothing

    _allInsp =

     

    Nothing

     

     

    End Sub

     

     

    Private Sub _allInsp_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles _allInsp.NewInspector

    objInspectors = Application.Inspectors

    _thisInsp = Inspector

     

     

    End Sub

     

     

    Private Sub _thisInsp_Activate() Handles _thisInsp.Activate 'Runs everytime the Inspector ( Window ) gains Focus.

    objInspector = _thisInsp

    SetupEdit()

     

     

    End Sub

     

     

    Sub SetupEdit()

     

     

    Dim objDoc As New Word.Document

     

     

    Dim objSel As Word.Selection

     

     

    For i = 1 To objInspectors.Count Step 1

     

     

    If objInspectors.Item(i).WordEditor IsNot Nothing Then 'This is never TRUE

    objInspector = objInspectors.Item(i)

    objDoc = objInspector.WordEditor

    objSel = objDoc.Windows(1).Selection

    myForm.UserControl41.TextBox4.AppendText(objSel.Text)

     

     

    Exit For

     

     

    End If

     

     

    Next i

     

     

    End Sub

    I am stumped...

     

    Monday, December 20, 2010 12:04 PM
  • I don't understand the purpose of most of your code. In NewInspector why not just test for a MailItem as Inspector.CurrentItem and set up for handling the item if true. Then in Activate() set a class level Word.Document object to Inspector.WordEditor.
     
    Dim objDoc As Word.Document
    Private Sub _thisInsp_Activate() Handles _thisInsp.Activate
        If (_thisInsp.WordEditor IsNot Nothing) Then
            objDoc = TryCast(_thisInsp.WordEditor, Word.Document)
        End If

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "Bill202" <=?utf-8?B?QmlsbDIwMg==?=> wrote in message news:35f8e4fc-c47a-41f3-8f7b-10333fd482cd...

    Sorry for the delay here but I had to get the rest of this done so I can get this one out the door, sans this little feature.

    I have been messing with this on and off for the past week and nothing works. I am wondering now if this could be something to do with Outllok setup. I have stopped all of the ancilary backround apps too.

    Here is what I am working with currently:

    Some of this is redundant, I know, just there for debugging.

    Imports

     

     

    Microsoft.Office.Interop

    Public

     

     

    Class ThisAddIn

     

     

    Public WithEvents _allInsp As Outlook.Inspectors

     

     

    Private WithEvents _thisInsp As Outlook.Inspector

     

     

    Private WithEvents _thisMail As Outlook.MailItem

     

     

     

    Protected Overrides Function CreateRibbonExtensibilityObject() As Office.IRibbonExtensibility

     

     

    Return Globals.Factory.GetRibbonFactory().CreateRibbonManager(New Microsoft.Office.Tools.Ribbon.IRibbonExtension() {New SiebelSave()})

     

     

    End Function

     

     

    Private Sub ThisAddIn_Startup() Handles Me.Startup

    _allInsp = Application.Inspectors

    objInspectors = _allInsp

     

     

    End Sub

     

     

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    objInspector =

     

    Nothing

    objInspectors =

     

    Nothing

    _thisInsp =

     

    Nothing

    _allInsp =

     

    Nothing

     

     

    End Sub

     

     

    Private Sub _allInsp_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles _allInsp.NewInspector

    objInspectors = Application.Inspectors

    _thisInsp = Inspector

     

     

    End Sub

     

     

    Private Sub _thisInsp_Activate() Handles _thisInsp.Activate 'Runs everytime the Inspector ( Window ) gains Focus.

    objInspector = _thisInsp

    SetupEdit()

     

     

    End Sub

     

     

    Sub SetupEdit()

     

     

    Dim objDoc As New Word.Document

     

     

    Dim objSel As Word.Selection

     

     

    For i = 1 To objInspectors.Count Step 1

     

     

    If objInspectors.Item(i).WordEditor IsNot Nothing Then 'This is never TRUE

    objInspector = objInspectors.Item(i)

    objDoc = objInspector.WordEditor

    objSel = objDoc.Windows(1).Selection

    myForm.UserControl41.TextBox4.AppendText(objSel.Text)

     

     

    Exit For

     

     

    End If

     

     

    Next i

     

     

    End Sub

    I am stumped...

     


    Ken Slovak MVP - Outlook
    Monday, December 20, 2010 4:27 PM
  • Hey Ken, almost all of it was added along the way just to watch what was going on. I did the For loop just to look at all the inspectors.

    This is what is in there now:

     

     

    Private Sub _allInsp_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles _allInsp.NewInspector

     _thisInsp = Inspector

     

     

    End Sub

     

     

    Private Sub _thisInsp_Activate() Handles _thisInsp.Activate

     

     

    If (_thisInsp.WordEditor IsNot Nothing) Then

    objDoc =

     

    TryCast(_thisInsp.WordEditor, Word.Document)

    SetupEdit()

     

     

    End If

    This doesn't work _thisInsp.WordEditor is always nothing.

    Monday, December 20, 2010 5:54 PM
  • You are positive that you are using Outlook 2007? Do you also have Word 2007 installed?
     
    Can you try this Outlook VBA code and see if it does anything? Open an email item and select the opened email, then use Alt+F11 to open the Outlook VBA project and expand the ThisOutlookSession class module. Use Tools, References to make sure that Word is referenced. Put this code in the class, put your cursor in the code and press F5 to run it. See what happens:
     
    Sub TestForWordMail()
        Dim oDoc As Word.Document
        Dim oInsp As Outlook.Inspector
     
        Set oInsp = Application.ActiveInspector
        If Not (oInsp.WordEditor Is Nothing) Then
            Set oDoc = oInsp.WordEditor
            MsgBox oDoc.ActiveWindow.Caption
        End If
    End Sub
       

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "Bill202" <=?utf-8?B?QmlsbDIwMg==?=> wrote in message news:bd1e7d2a-91a6-4b8b-bcac-eae8e0ad4c8d...

    Hey Ken, almost all of it was added along the way just to watch what was going on. I did the For loop just to look at all the inspectors.

    This is what is in there now:

     

     

    Private Sub _allInsp_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles _allInsp.NewInspector

     _thisInsp = Inspector

     

     

    End Sub

     

     

    Private Sub _thisInsp_Activate() Handles _thisInsp.Activate

     

     

    If (_thisInsp.WordEditor IsNot Nothing) Then

    objDoc =

     

    TryCast(_thisInsp.WordEditor, Word.Document)

    SetupEdit()

     

     

    End If

    This doesn't work _thisInsp.WordEditor is always nothing.


    Ken Slovak MVP - Outlook
    Monday, December 20, 2010 7:58 PM
  • Yes both Outlook and Word 2007. However you may be onto something VBA woun't lounch. Let me dig into that...
    Monday, December 20, 2010 8:40 PM
  • Sorry, forgot I had it disabled.

    Interesting, I get a Runtime error: 6158

    This Object Model Command is not available in email

    Monday, December 20, 2010 9:08 PM
  • I ran a Repair on MS Office and it seems to be working now.

    BTW it's this that generated the error: MsgBox oDoc.ActiveWindow.Caption

    Rest of it works fine.

     

    Ken I can't thank you enough, got me pointed in a different direction that unwrapped me from the axel.

    Monday, December 20, 2010 9:17 PM