Visio Guy

Visio Discussions => Programming & Code => Topic started by: AndyW on June 16, 2022, 09:14:44 AM

Title: RegisterRibbonX from c#
Post by: AndyW on June 16, 2022, 09:14:44 AM
Has anyone had any success in using RegisterRibbonX from c#. All I get is a COMException with 'Invalid Parameter'. I am trying to automate a Visio appliction (no an addin).

diagramRibbon= newMyApp.Library.DiagramRibbon();

visioApp.RegisterRibbonX(diagramRibbon, visioDoc, Visio.VisRibbonXModes.visRXModeDrawing, "Diagram Ribbon");


If I call this from VBA it works, so my DiagramRibbon class seems okay, so it seems Interop is affecting this.
Title: Re: RegisterRibbonX from c#
Post by: Nikolay on June 16, 2022, 12:37:54 PM
I am not sure if RegisterRibbbonX works for out-of-process scenario like yours
What kind of object does newMyApp.Library.DiagramRibbon() return?
It should implement idispatch and iribbonextensibility interfaces at least, as far as I understand?
In c# terms, should be marked as ComVisible(true) and implement the method the Interface to return the ribbon xml
Probably it should be doable though. Ad far as I remember Visio event monitor (from SDK) is a stand-alone app (exe) and has a ribbon. If I remember correctly of course:)
Title: Re: RegisterRibbonX from c#
Post by: AndyW on June 16, 2022, 01:15:24 PM
Hmm, I had no considered the out-of-process aspect. My ribbon class works if I call it with VBA from a Visio diagram

namespace MyApp.Library
{
    [ComVisible(true)]
    [Guid("0577A201-E8BF-451F-B464-7F558F2C29E7")]
    public class DiagramRibbon : Office.IRibbonExtensibility
    {
        public DiagramRibbon() { }

        public string GetCustomUI(string RibbonID)
        {
            StreamReader customUIReader = new System.IO.StreamReader("D:\\App\\Ribbon_Diagram.xml");
            string customUIData = customUIReader.ReadToEnd();
            return customUIData;
        }
    }
}
Title: Re: RegisterRibbonX from c#
Post by: Nikolay on June 16, 2022, 09:16:54 PM
Could it be that you create a temporary variable "diagramRibbon" and then it is immediately destroyed when the function exists the scope?
For me, your code does not seem to throw exceptions at least.