public static MorphicResult <MorphicUnit, MorphicUnit> EnableEssentialsSimplifyRibbon()
        {
            var disableRibbonResult = WordRibbon.EnableRibbon(WordRibbon.ESSENTIALS_SIMPLIFY_RIBBON_ID, WordRibbon.BASIC_SIMPLIFY_RIBBON_ID /* insertAfterRibbonId */);

            if (disableRibbonResult.IsError == true)
            {
                return(MorphicResult.ErrorResult());
            }

            // NOTE: we have removed this functionality for the time being due to limitations in Word automation; we may revisit it in the future
            //await WordRibbon.ReloadRibbons();

            return(MorphicResult.OkResult());
        }
        //

        public static MorphicResult <MorphicUnit, MorphicUnit> DisableBasicSimplifyRibbon()
        {
            var disableRibbonResult = WordRibbon.DisableRibbon(WordRibbon.BASIC_SIMPLIFY_RIBBON_ID);

            if (disableRibbonResult.IsError == true)
            {
                return(MorphicResult.ErrorResult());
            }

            // NOTE: we have removed this functionality for the time being due to limitations in Word automation; we may revisit it in the future
            //await WordRibbon.ReloadRibbonsAsync();

            return(MorphicResult.OkResult());
        }
        private static MorphicResult <XmlNode, MorphicUnit> GetRibbonTabNodeFromTemplate(string ribbonId)
        {
            var ribbonTemplateFileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Morphic.Integrations.Office.Templates.Word_ComponentTemplates.xml");

            if (ribbonTemplateFileStream is null)
            {
                return(MorphicResult.ErrorResult());
            }

            var xmlDocument = new XmlDocument();

            try
            {
                xmlDocument.Load(ribbonTemplateFileStream);
            }
            catch
            {
                return(MorphicResult.ErrorResult());
            }

            var ribbonTabNodeResult = WordRibbon.GetRibbonTabNodeFromXmlDocument(xmlDocument, ribbonId);

            if (ribbonTabNodeResult.IsError == true)
            {
                return(MorphicResult.ErrorResult());
            }
            var ribbonTabNode = ribbonTabNodeResult.Value;

            if (ribbonTabNode is null)
            {
                // if we did not find the tab in our list, return an error
                return(MorphicResult.ErrorResult());
            }
            else
            {
                return(MorphicResult.OkResult(ribbonTabNode !));
            }
        }
        //

        public static MorphicResult <bool, MorphicUnit> IsBasicSimplifyRibbonEnabled()
        {
            return(WordRibbon.IsRibbonEnabled(WordRibbon.BASIC_SIMPLIFY_RIBBON_ID));
        }
 private static string GetPathToWordRibbonFile()
 {
     return(System.IO.Path.Combine(WordRibbon.GetPathToOfficeUserDataDirectory(), "Word.officeUI"));
 }
        // NOTE: if we add more Word- or Office-related functionality, we should move this region to a separate class
        public static bool IsOfficeInstalled()
        {
            var path = WordRibbon.GetPathToOfficeUserDataDirectory();

            return(System.IO.Directory.Exists(path));
        }
        // NOTE: this function does not make Word update its ribbons in real-time; to do so, call the ReloadRibbons() function after using this function
        private static MorphicResult <MorphicUnit, MorphicUnit> EnableRibbon(string ribbonId, string?insertAfterRibbonId = null)
        {
            XmlDocument xmlDocument = new XmlDocument();

            var path = GetPathToWordRibbonFile();

            if (System.IO.File.Exists(path) == true)
            {
                try
                {
                    xmlDocument.Load(path);
                }
                catch
                {
                    return(MorphicResult.ErrorResult());
                }
            }

            XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);

            xmlNamespaceManager.AddNamespace("mso", WordRibbon.MSO_NAMESPACE);

            // verify that the "mso:customUI" node exists
            var msoCustomUINode = xmlDocument.SelectSingleNode("mso:customUI", xmlNamespaceManager);

            if (msoCustomUINode is null)
            {
                // required root note doesn't exist

                // does a DIFFERENT root node exist?  If so, exit with failure
                var rootNode = xmlDocument.FirstChild;
                if (rootNode is not null)
                {
                    return(MorphicResult.ErrorResult());
                }

                // otherwise, if the XML tree is empty, create the required root node
                msoCustomUINode = xmlDocument.CreateNode(XmlNodeType.Element, "mso:customUI", WordRibbon.MSO_NAMESPACE);
                xmlDocument.AppendChild(msoCustomUINode);
            }

            // verify that the "mso:customUI/mso:ribbon" node exists
            var msoRibbonNode = xmlDocument.SelectSingleNode("mso:customUI/mso:ribbon", xmlNamespaceManager);

            if (msoRibbonNode is null)
            {
                // required ribbon node doesn't exist; create it now
                msoRibbonNode = xmlDocument.CreateNode(XmlNodeType.Element, "mso:ribbon", WordRibbon.MSO_NAMESPACE);
                msoCustomUINode.AppendChild(msoRibbonNode);
            }

            // verify that the "mso:customUI/mso:ribbon/mso:tabs" node exists
            var msoTabsParentNode = xmlDocument.SelectSingleNode("mso:customUI/mso:ribbon/mso:tabs", xmlNamespaceManager);

            if (msoTabsParentNode is null)
            {
                // required tabs node doesn't exist; create it now
                msoTabsParentNode = xmlDocument.CreateNode(XmlNodeType.Element, "mso:tabs", WordRibbon.MSO_NAMESPACE);
                msoRibbonNode.AppendChild(msoTabsParentNode);
            }

            // verify that the tab (ribbon) is not already present; if it is, then return success
            var msoTabNodes = xmlDocument.SelectNodes("mso:customUI/mso:ribbon/mso:tabs/mso:tab", xmlNamespaceManager);

            if (msoTabNodes is not null)
            {
                foreach (XmlNode?msoTabNode in msoTabNodes !)
                {
                    if (msoTabNode?.Attributes["id"].Value == ribbonId)
                    {
                        return(MorphicResult.OkResult());
                    }
                }
            }

            // at this point, we know that the tab (i.e. ribbon) is not in the XmlDocument; load it from our template and insert it now

            // get a copy of the appropriate tab (ribbon) node from the template we have embedded as a resource in this library
            var getRibbonNodeResult = WordRibbon.GetRibbonTabNodeFromTemplate(ribbonId);

            if (getRibbonNodeResult.IsError == true)
            {
                // programming error
                throw new Exception("Error: could not get ribbon (resource '" + ribbonId + "')");
            }
            var ribbonNodeToImport = getRibbonNodeResult.Value !;

            XmlNode?insertAfterNode = null;

            if (insertAfterRibbonId is not null)
            {
                var getRibbonResult = GetRibbonTabNodeFromXmlDocument(xmlDocument, insertAfterRibbonId !);
                if ((getRibbonResult.IsSuccess == true) && (getRibbonResult.Value is not null))
                {
                    insertAfterNode = getRibbonResult.Value !;
                }
            }

            // insert the tab (ribbon) node at the top of our list (or in the appropriate place, if it should go _after_ another node
            // TODO: we actually want to insert ESSENTIALS _after_ BASIC if BASIC exists; add some logic for that!
            var ribbonNode = xmlDocument.ImportNode(ribbonNodeToImport, true);

            if (insertAfterNode is not null)
            {
                msoTabsParentNode.InsertAfter(ribbonNode, insertAfterNode !);
            }
            else
            {
                msoTabsParentNode.InsertBefore(ribbonNode, msoTabsParentNode.FirstChild);
            }

            // save out the modified XMLdocument
            try
            {
                xmlDocument.Save(path);
            }
            catch
            {
                return(MorphicResult.ErrorResult());
            }

            // if we reach here, the ribbon has been enabled; return success
            return(MorphicResult.OkResult());
        }
 public static MorphicResult <bool, MorphicUnit> IsEssentialsSimplifyRibbonEnabled()
 {
     return(WordRibbon.IsRibbonEnabled(WordRibbon.ESSENTIALS_SIMPLIFY_RIBBON_ID));
 }