-1

I wrote an AddIn for Word (C#, VSTO). If I open a Wordfile in the started Word-Instance (Office 2016 Professional Plus) the registered DocumentOpen Event is raised. But if I start Word via Doubleclick on a Wordfile, the Event will be ignored.

I tried to put the Code of the function Ribbon_Load at the ThisAddIn_Startup function in the ThisAddIn.cs Class. But the behavior is still the same: The Event DocumentOpen is not raised when Word is started via a Doubleclick on a Wordfile.

Here´s my reproducible example:

Class LAS-Vorlagen.cs:

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;

namespace LAS_Word_Vorlagen
{
    [ComVisible(true)]
    public class LAS_Vorlagen : Office.IRibbonExtensibility
    { 
        private Office.IRibbonUI ribbon;

        public LAS_Vorlagen()
        {
        }

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("LAS_Word_Vorlagen.LAS-Vorlagen.xml");
        }

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
            // Event-Listener registration
           // Globals.ThisAddIn.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(MessageBox_Show_At_Loading_Document);
        }

        public void MessageBox_Show_At_Loading_Document(Document doc)
        {
            MessageBox.Show("DocumentOpen-Event raised!");
        }

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }
    }
}

Here´s the ThisAddIn.cs class:

using Word = Microsoft.Office.Interop.Word;

namespace LAS_Word_Vorlagen
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            // Event-Listener registration
            LAS_Vorlagen test = new LAS_Vorlagen();
            Globals.ThisAddIn.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(test.MessageBox_Show_At_Loading_Document);
        }

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

        #region Von VSTO generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion

        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new LAS_Vorlagen();
        }
    }
}

And finally the XML-Configuration LAS-Vorlagen.xml:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab id="TabAddIns_LAS"  label ="LAS-Vorlagen">
      </tab>
    </tabs>
  </ribbon>
</customUI>
AndyE
  • 1
  • 1
  • Not so much a bug as how Word and Windows are designed... Could you try using the application event handler DocumentOpen? – Cindy Meister Nov 08 '19 at 14:15
  • @CindyMeister I already use the "DocumentOpen" Handler since I Register it when die "Ribbon_Load" Function is called - see my example above. But the "Ribbon_Load"- function is only called when I open a document in an (empty) Word instance. The "Ribbon_Load" isn´t called if I open a document via doubleclick on a wordfile. This sounds strange I know - especally since my Ribbon is available in word all the time, but the Event is not registered as it should be. – AndyE Nov 09 '19 at 13:46
  • Don't register `DocumentOpen` in `RibbonLoad`, register it in `ThisAddin_Startup`. That should let you "nudge" the Ribbon when a document opens. – Cindy Meister Nov 09 '19 at 14:18
  • @CindyMeister unfortunately this didn´t work out as I wish to. Please take a short look at **Update 2** in my Question above. – AndyE Nov 11 '19 at 15:36
  • The information in the question is not presented in a way that allows anyone to test - the question is lacking a [mcve]. What I can say, however, is that I don't understand the last statement about the non-static function. This is a `public void` (meaning it returns nothing and can therefore not be a function) registered to handle `DocumentOpen`, so is automatically in the current instance of the VSTO add-in? – Cindy Meister Nov 11 '19 at 17:26
  • @CindyMeister sorry I made a mistake while testing so my comment About my non Static method was wrong. I also changed my Question to a reproducible example. I hope my Problem can now be better understood. – AndyE Nov 13 '19 at 06:54

1 Answers1

-1

You need to use ribbon callbacks if you want to get the Fluent UI updated dynamically. For each of the callbacks the add-in implements, the responses are cached. For example, if an add-in writer implements the getImage callback procedure for a button, the function is called once, the image loads, and then if the image needs to be updated, the cached image is used instead of recalling the procedure. This process remains in-place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.

The following method can be helpful in such case:

public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
            this.ribbon = ribbonUI;  
}

Then when required you may call the methods specified above to get cached values updated by calling ribbon callbacks:

this.ribbon.Invalidate()

Read more about the Fluent UI (aka Ribbon UI) in the following series of articles:

FYI By default, if a VSTO Add-in attempts to manipulate the Microsoft Office user interface (UI) and fails, no error message is displayed. However, you can configure Microsoft Office applications to display messages for errors that relate to the UI. You can use these messages to help determine why a custom ribbon does not appear, or why a ribbon appears but no controls appear.

To show VSTO Add-in user interface errors

  1. Start the application.
  2. Click the File tab.
  3. Click Options.
  4. In the categories pane, click Advanced.
  5. In the details pane, select Show VSTO Add-in user interface errors, and then click OK.

Read more about that in the How to: Show Add-in user interface errors article.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45