none
Exceptions when using Office and OneNote interops in a OneNote add-in RRS feed

  • Question

  • Hello,

    Consider a sample OneNote add-in project given at https://github.com/OneNoteDev/VanillaAddIn.

    First, I modify the project as described in readme; I assume you may skip this step if you are going to perform a limited testing only. Then I modify the Ribbon XML by adding two Ribbon buttons and writing the following methods to be called by these buttons; the event handlers show the number of windows opened; they use early binding and late binding correspondingly:

    public void myButtonClicked(IRibbonControl control) { try { MessageBox.Show("myButtonClicked: " + OneNoteApplication.Windows.Count.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } } public void myButton2Clicked(IRibbonControl control) { uint count = 0; object windows = null; try { windows = OneNoteApplication.GetType().InvokeMember("Windows", System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public, null, OneNoteApplication, new object[] { }); if (windows != null) { object objCount = windows.GetType().InvokeMember("Count", System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public, null, windows, new object[] { }); if (objCount != null) count = Convert.ToUInt32(objCount); } MessageBox.Show("myButton2Clicked: " + count.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } }

    The final touch: remove references to OneNote and Office interops and add references to OneNote and Office interop assemblies located in this folder (see the Visual Studio 2017 installation folder):

    C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office15\

    Build the add-in. You may need to set Embed Interop = False on the OneNote reference. Build the setup project. Install the add-in. Start OneNote. Click the first button. For me, it produces the following exception:

    Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.OneNote.IApplication'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{A47D3223-06A5-3DD6-916F-50E803E0AFC2}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
    
       at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
    
       at Microsoft.Office.Interop.OneNote.IApplication.get_Windows()
    
       at MyApplication.TestAddIn.AddIn.myButtonClicked(IRibbonControl control)

    Clicking the second button produces a different exception:

    Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))
    
       at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
    
       at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
    
       at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
    
       at MyApplication.TestAddIn.AddIn.myButton2Clicked(IRibbonControl control)

    We have this issue on several machines. We also reproduce this issue if we use interops for Office 2010/2013.

    A seemingly related variant of the issue can be reproduced using another sample project - https://code.msdn.microsoft.com/CSOneNoteRibbonAddIn-c3547362. The project doesn't require customizing - just build the project and installer. If built and installed on a machine with Office 2010, it works. If you run this installer on a machine with Office 2013/2016, the add-in produces the same exceptions:

    early binding:

    System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.OneNote.IApplication'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{A47D3223-06A5-3DD6-916F-50E803E0AFC2}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
       at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
       at Microsoft.Office.Interop.OneNote.IApplication.get_Windows()
       at CSOneNoteRibbonAddIn.TestForm.GetActiveObjectID(_ObjectType obj)
       at CSOneNoteRibbonAddIn.TestForm.GetActivePageContent()
       at CSOneNoteRibbonAddIn.TestForm.GetPageTitle()
       at CSOneNoteRibbonAddIn.TestForm.btnClick_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2098.0 built by: NET47REL1LAST
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    CSOneNoteRibbonAddIn
        Assembly Version: 1.0.6402.20503
        Win32 Version: 1.0.6402.20503
        CodeBase: file:///C:/Program%20Files%20(x86)/Microsoft/CSOneNoteRibbonAddIn/CSOneNoteRibbonAddIn.dll
    ----------------------------------------
    ...
    ----------------------------------------
    Microsoft.Office.Interop.OneNote
        Assembly Version: 15.0.0.0
        Win32 Version: 15.0.4420.1017
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.Office.Interop.OneNote/15.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.OneNote.dll
    ----------------------------------------
    

    late binding:

    System.Runtime.InteropServices.COMException (0x8002801D): Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))
       at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
       at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
       at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
       at CSOneNoteRibbonAddIn.TestForm.GetActiveObjectID(_ObjectType obj)
       at CSOneNoteRibbonAddIn.TestForm.GetActivePageContent()
       at CSOneNoteRibbonAddIn.TestForm.GetPageTitle()
       at CSOneNoteRibbonAddIn.TestForm.btnClick_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2098.0 built by: NET47REL1LAST
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    CSOneNoteRibbonAddIn
        Assembly Version: 1.0.6402.21490
        Win32 Version: 1.0.6402.21490
        CodeBase: file:///C:/Program%20Files%20(x86)/Microsoft/CSOneNoteRibbonAddIn/CSOneNoteRibbonAddIn.dll
    ----------------------------------------
    ...

    Do you have any suggestions? Do I miss something simple?

    On my machine I have Office 2016 Insider Fast. A regular Office 2016 is installed on other machines.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Thursday, July 13, 2017 1:46 PM

Answers

  • Andrei,

    Yes, you can use this workaround in OneNote2010.

    As of now, we do not have any documentation describing this as a known issue. I will request for one internally.


    Friday, August 4, 2017 6:42 PM

All replies

  • I wonder if I posted this question to a wrong forum.

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Tuesday, July 18, 2017 9:23 AM
  • Hi Andrei Smolin,
    Thank you for posting in the MSDN Forum.
    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.
    Sorry for any inconvenience and have a nice day! 
    Best Regards,
    Terry
    Friday, July 21, 2017 3:38 AM
  • Hi Andrei,

    This is a known issue in OneNote.

    Fortunately, We a have a workaround for this issue. Workaround is to not use the Application Object that OneNote hands over to the add-in's onConnection event handle, but create a new one. Since OneNote is singleton, it would not start a new OneNote process and would give us the reference to the same existing Application object. This approach would not be appropriate in the context of other applications like Word, Excel, but since OneNote is a singleton, this works.

    Wednesday, August 2, 2017 7:33 PM
  • Hello Jeeva,

    Thank you for your response!

    Can I use this workaround when in OneNote 2010? Is there a resource describing this known issue?

    Thank you again!


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Thursday, August 3, 2017 1:56 PM
  • Andrei,

    Yes, you can use this workaround in OneNote2010.

    As of now, we do not have any documentation describing this as a known issue. I will request for one internally.


    Friday, August 4, 2017 6:42 PM
  • Thank you, Jeeva!

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Wednesday, August 30, 2017 12:44 PM