none
How to register events in One note 2016 addin using c++? RRS feed

  • Question

  • Hi

    Has Anybody tried with registering events in one note addin developed in c++.

    I have tried registering events in Office 2016 in c#,it works but I get delayed response for the actions performed. As mentioned in the documentation i.e. events are not supported in managed code so can I get the sample to register events in cpp. Any help will be appreciated.

    Thanks,

    Vinod

    Tuesday, July 18, 2017 10:45 AM

All replies

  • Hi Vinod,

    >> Has Anybody tried with registering events in one note addin developed in c++

    Which event do you want to register? Do you want to register event to OneNote Object Model or your custom object?

    >> it works but I get delayed response for the actions performed

    Could you share us how you register events in C#?

    >> As mentioned in the documentation i.e. events are not supported in managed code

    It would be helpful if you could share us this document.

    In addition, could you share us a simple demo which could reproduce your issue in OneNote C# addin? We will try to reproduce your issue.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 19, 2017 2:22 AM
  • Hi  Edward,

    Thanks for reply.

    Which event do you want to register? Do you want to register event to OneNote Object Model or your custom object?

    I want to register following events :- 1.OnNavigate and 2.OnHierarchyChange.

    I have registered as follows:

    /****************************** Module Header ******************************\
    Module Name:  Connect.cs
    Project:      CSOneNoteRibbonAddIn
    Copyright (c) Microsoft Corporation.

    Hosts the event notifications that occur to add-ins, such as when they are loaded, 
    unloaded, updated, and so forth.

    This source is subject to the Microsoft Public License.
    See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
    All other rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
    EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
    WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
    \***************************************************************************/

    namespace CSOneNoteRibbonAddIn
    {
        #region Imports directives
        using System;
        using Extensibility;
        using System.Runtime.InteropServices;
        using System.Windows.Forms;
        using Microsoft.Office.Core;
        using System.Runtime.InteropServices.ComTypes;
        using System.IO;
        using CSOneNoteRibbonAddIn.Properties;
        using System.Drawing.Imaging;
        using OneNote = Microsoft.Office.Interop.OneNote; 
        #endregion

        #region Read me for Add-in installation and setup information.
        // When run, the Add-in wizard prepared the registry for the Add-in.
        // At a later time, if the Add-in becomes unavailable for reasons such as:
        //   1) You moved this project to a computer other than which is was originally created on.
        //   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
        //   3) Registry corruption.
        // you will need to re-register the Add-in by building the CSOneNoteRibbonAddInSetup project, 
        // right click the project in the Solution Explorer, then choose install.
        #endregion

        /// <summary>
        ///   The object for implementing an Add-in.
        /// </summary>
        /// <seealso class='IDTExtensibility2' />
        [GuidAttribute("0BE84534-48A5-48A7-A9BD-0B5CAE7E12A0"),
        ProgId("CSOneNoteRibbonAddIn.Connect")]
        public class Connect : Object, Extensibility.IDTExtensibility2, IRibbonExtensibility
        {
            /// <summary>
            /// Implements the constructor for the Add-in object.
            /// Place your initialization code within this method.
            /// </summary>
            public Connect()
            {
    #if DEBUG

                System.Diagnostics.Debugger.Launch();

    #endif
            }

            /// <summary>
            ///      Implements the OnConnection method of the IDTExtensibility2 interface.
            ///      Receives notification that the Add-in is being loaded.
            /// </summary>
            /// <param term='application'>
            ///      Root object of the host application.
            /// </param>
            /// <param term='connectMode'>
            ///      Describes how the Add-in is being loaded.
            /// </param>
            /// <param term='addInInst'>
            ///      Object representing this Add-in.
            /// </param>
            /// <seealso class='IDTExtensibility2' />
            public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, 
                object addInInst, ref System.Array custom)
            {
                MessageBox.Show("CSOneNoteRibbonAddIn OnConnection");
                applicationObject = application;
                addInInstance = addInInst;
                try
                {
                    var app = (application as OneNote.Application);
                    if (app != null)
                    {
                        //app.OnNavigate += App_OnNavigate;
                        app.OnHierarchyChange += Connect_OnHierarchyChange;
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(string.Format("Failed as {0}",ex.Message));

                }
            }

            private void App_OnNavigate()
            {
                MessageBox.Show("CSOneNoteRibbonAddIn App_OnNavigate");
            }

            private void Connect_OnHierarchyChange(string bstrActivePageID)
            {
               // (this.applicationObject as OneNote.Application).GetHierarchyParent(bstrActivePageID,out )
                MessageBox.Show(string.Format("Moved to page {0}", bstrActivePageID));
            }

            /// <summary>
            ///     Implements the OnDisconnection method of the IDTExtensibility2 interface.
            ///     Receives notification that the Add-in is being unloaded.
            /// </summary>
            /// <param term='disconnectMode'>
            ///      Describes how the Add-in is being unloaded.
            /// </param>
            /// <param term='custom'>
            ///      Array of parameters that are host application specific.
            /// </param>
            /// <seealso class='IDTExtensibility2' />
            public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, 
                ref System.Array custom)
            {
                MessageBox.Show("CSOneNoteRibbonAddIn OnDisconnection");
                this.applicationObject = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            /// <summary>
            ///      Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
            ///      Receives notification that the collection of Add-ins has changed.
            /// </summary>
            /// <param term='custom'>
            ///      Array of parameters that are host application specific.
            /// </param>
            /// <seealso class='IDTExtensibility2' />
            public void OnAddInsUpdate(ref System.Array custom)
            {
                MessageBox.Show("CSOneNoteRibbonAddIn OnAddInsUpdate");
            }

            /// <summary>
            ///      Implements the OnStartupComplete method of the IDTExtensibility2 interface.
            ///      Receives notification that the host application has completed loading.
            /// </summary>
            /// <param term='custom'>
            ///      Array of parameters that are host application specific.
            /// </param>
            /// <seealso class='IDTExtensibility2' />
            public void OnStartupComplete(ref System.Array custom)
            {
                MessageBox.Show("CSOneNoteRibbonAddIn OnStartupComplete");
            }

            /// <summary>
            ///      Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
            ///      Receives notification that the host application is being unloaded.
            /// </summary>
            /// <param term='custom'>
            ///      Array of parameters that are host application specific.
            /// </param>
            /// <seealso class='IDTExtensibility2' />
            public void OnBeginShutdown(ref System.Array custom)
            {
                MessageBox.Show("CSOneNoteRibbonAddIn OnBeginShutdown");

                if (this.applicationObject != null)
                {
                    this.applicationObject = null;
                }
            }

            private object applicationObject;
            private object addInInstance;

    It is mentioned in below mentioned sites that events are not supported in managed code.

    • http://msdn.microsoft.com/en-us/library/gg649853.aspx

    Thanks,

    Vinod

    Thursday, July 20, 2017 10:25 AM
  • Hi Vinod,

    Thanks for sharing more information.

    For OnNavigate and OnHierarchyChange, they are events for OneNote application. I think you could register them in cpp.

    But, in my option, if you get delayed response for actions performed in C#, you will get the same result in cpp. These events reply on OneNote application, I think it is not related with which language you implement them.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 21, 2017 6:01 AM