none
How to open an existing word/excel 2010 with custom template

    Question

  • HI,

    Anybody can help me? now i knew custom word/excel 2010 UI by creating office template/documents/Add-in. and they are builded as .dll,  how could i use this .dll or other ways to open an existing word/excel with customed UI.

    Thx in advance.

    Wednesday, December 12, 2012 8:06 AM

Answers

  • Hi "after base",

    AddRibbon.cs is used to add Ribbon, please pay attention to CreateRibbon method, it is the key process to add ribbon and other content you can omit:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using com.mksword.Net.OpenXmlTools;
    using com.mksword.OpenXmlUtil_log4netTool;
    using DocumentFormat.OpenXml.Office.CustomUI;
    namespace WindowsFormsApplication8
    {
        public class AddRibbon : WordTool
        {
            public override void Action()
            {
                try
                {
                    if (this._Core.HandlerDocumentLocal(CreateRibbon,true))
                    {
                        SetLog("Ribbon has been added", OXULogType.INFO);
                    }
                    else
                    {
                        SetLog("Ribbon can't be added", OXULogType.FATAL);
                    }
                }
                catch (Exception ex)
                {
                    SetLog(ex, OXULogType.FATAL);
                }
            }
            private bool CreateRibbon(WordprocessingDocument WDP)
            {
                bool result = false;
                MainDocumentPart MDP = WDP.MainDocumentPart;
                try
                {
                    RibbonExtensibilityPart REP = WDP.AddRibbonExtensibilityPart();
                    string xml = "<customUI xmlns=\""
                        + "http://schemas.microsoft.com/office/2006/01/customui\" "
                        + "><ribbon><tabs><tab "
                        + "idMso=\"TabAddIns\"><group id=\"MyGroup\" label=\""
                        + "My Group\"><button id=\"Test\" label=\"Test\"/>"
                        + "</group></tab></tabs></ribbon></customUI>";
                    REP.CustomUI = new CustomUI(xml);
                    MDP.Document.Save();
                    result = true;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.TraceInformation(ex.Message);
                    SetLog(ex, OXULogType.FATAL);
                }
                return result;
            }
        }
        
    }

    RemoveRibbon.cs is used to remove ribbon. Please pay attention to DeleteRibbon method and other content please omit:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using com.mksword.Net.OpenXmlTools;
    using com.mksword.OpenXmlUtil_log4netTool;
    using DocumentFormat.OpenXml.Office.CustomUI;
    namespace WindowsFormsApplication8
    {
        public class RemoveRibbon : WordTool
        {
            public override void Action()
            {
                try
                {
                    _Core.HandlerDocumentLocal(DeleteRibbon, true);
                }
                catch (Exception ex)
                {
                    SetLog(ex, OXULogType.FATAL);
                }
            }
            private bool DeleteRibbon(WordprocessingDocument WDP)
            {
                bool result = false;
                try
                {
                    RibbonExtensibilityPart REP = WDP.RibbonExtensibilityPart;
                    if (WDP.DeletePart(REP))
                    {
                        WDP.MainDocumentPart.Document.Save();
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    SetLog(ex, OXULogType.FATAL);
                }
                return result;
            }
        }
    }

    This kind of work round only work with Open Xml formatted document.

    After this process you need add VBA module to finish your business logic. Additional information please see: http://blogs.msdn.com/b/brian_jones/archive/2009/08/03/the-open-xml-sdk-and-fluent-ui-extensibility.aspx

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Tom_Xu_WXModerator Monday, December 17, 2012 6:34 AM
    • Proposed as answer by Tom_Xu_WXModerator Wednesday, December 19, 2012 2:39 AM
    • Marked as answer by after_base Wednesday, December 19, 2012 3:39 AM
    • Unmarked as answer by after_base Friday, December 21, 2012 6:20 AM
    • Marked as answer by after_base Sunday, December 23, 2012 11:13 AM
    Monday, December 17, 2012 6:32 AM
    Moderator

All replies

  • Hi "after base",

    Thanks for posting in the MSDN Forum.

    I would recommend your clarify which kind of UI you want to customized. As usual, we will customize the ribbon, context menu for Word or Excel. And the simple way to approach your goal is create add-in to handle it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 13, 2012 4:57 AM
    Moderator
  • Hi "after base",

    Thanks for posting in the MSDN Forum.

    I would recommend your clarify which kind of UI you want to customized. As usual, we will customize the ribbon, context menu for Word or Excel. And the simple way to approach your goal is create add-in to handle it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Hi Tom

    I have done it as you said, but all the word/excel work like custom UI, that is not my excpecte. I just want the word which is open by my app works like custom UI.

    I'd like to disable  Save,Open,Save as commands in my app, not all the word/excel documents.

    Best Regards,

    After Base

    Thursday, December 13, 2012 7:39 AM
  • Hi "after base",

    I suppose you program is a Window form application or WPF application. I think it is a way to solve your request via RibbonX and Open Xml SDK. You can disable the control via RibbonXml content and use Open Xml sdk to set it to the specific Word document. And remove the ribbonXml content when you close the program.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 14, 2012 6:18 AM
    Moderator
  • Hi "after base",

    I suppose you program is a Window form application or WPF application. I think it is a way to solve your request via RibbonX and Open Xml SDK. You can disable the control via RibbonXml content and use Open Xml sdk to set it to the specific Word document. And remove the ribbonXml content when you close the program.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Hi Tom,

    my program is a window form application, the qustion is how to open RibbonX and open Xml SDK and set it to the specific Word document. Can you give me a excample?

    Thanks,

    AFTER BASE


    • Edited by after_base Monday, December 17, 2012 12:59 AM
    Monday, December 17, 2012 12:41 AM
  • Hi "after base",

    AddRibbon.cs is used to add Ribbon, please pay attention to CreateRibbon method, it is the key process to add ribbon and other content you can omit:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using com.mksword.Net.OpenXmlTools;
    using com.mksword.OpenXmlUtil_log4netTool;
    using DocumentFormat.OpenXml.Office.CustomUI;
    namespace WindowsFormsApplication8
    {
        public class AddRibbon : WordTool
        {
            public override void Action()
            {
                try
                {
                    if (this._Core.HandlerDocumentLocal(CreateRibbon,true))
                    {
                        SetLog("Ribbon has been added", OXULogType.INFO);
                    }
                    else
                    {
                        SetLog("Ribbon can't be added", OXULogType.FATAL);
                    }
                }
                catch (Exception ex)
                {
                    SetLog(ex, OXULogType.FATAL);
                }
            }
            private bool CreateRibbon(WordprocessingDocument WDP)
            {
                bool result = false;
                MainDocumentPart MDP = WDP.MainDocumentPart;
                try
                {
                    RibbonExtensibilityPart REP = WDP.AddRibbonExtensibilityPart();
                    string xml = "<customUI xmlns=\""
                        + "http://schemas.microsoft.com/office/2006/01/customui\" "
                        + "><ribbon><tabs><tab "
                        + "idMso=\"TabAddIns\"><group id=\"MyGroup\" label=\""
                        + "My Group\"><button id=\"Test\" label=\"Test\"/>"
                        + "</group></tab></tabs></ribbon></customUI>";
                    REP.CustomUI = new CustomUI(xml);
                    MDP.Document.Save();
                    result = true;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.TraceInformation(ex.Message);
                    SetLog(ex, OXULogType.FATAL);
                }
                return result;
            }
        }
        
    }

    RemoveRibbon.cs is used to remove ribbon. Please pay attention to DeleteRibbon method and other content please omit:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using com.mksword.Net.OpenXmlTools;
    using com.mksword.OpenXmlUtil_log4netTool;
    using DocumentFormat.OpenXml.Office.CustomUI;
    namespace WindowsFormsApplication8
    {
        public class RemoveRibbon : WordTool
        {
            public override void Action()
            {
                try
                {
                    _Core.HandlerDocumentLocal(DeleteRibbon, true);
                }
                catch (Exception ex)
                {
                    SetLog(ex, OXULogType.FATAL);
                }
            }
            private bool DeleteRibbon(WordprocessingDocument WDP)
            {
                bool result = false;
                try
                {
                    RibbonExtensibilityPart REP = WDP.RibbonExtensibilityPart;
                    if (WDP.DeletePart(REP))
                    {
                        WDP.MainDocumentPart.Document.Save();
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    SetLog(ex, OXULogType.FATAL);
                }
                return result;
            }
        }
    }

    This kind of work round only work with Open Xml formatted document.

    After this process you need add VBA module to finish your business logic. Additional information please see: http://blogs.msdn.com/b/brian_jones/archive/2009/08/03/the-open-xml-sdk-and-fluent-ui-extensibility.aspx

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Tom_Xu_WXModerator Monday, December 17, 2012 6:34 AM
    • Proposed as answer by Tom_Xu_WXModerator Wednesday, December 19, 2012 2:39 AM
    • Marked as answer by after_base Wednesday, December 19, 2012 3:39 AM
    • Unmarked as answer by after_base Friday, December 21, 2012 6:20 AM
    • Marked as answer by after_base Sunday, December 23, 2012 11:13 AM
    Monday, December 17, 2012 6:32 AM
    Moderator
  • Hi "after base",

    No, that is a assembly which I created to warp some frequently process for Open xml operation. It's not necessary for Open Xml program. the key code is in the CreateRibbon method and DeleteRibbon method. You can use the code in those method in your program to handle your issue. And the reference which I provided is very important for your issue. The code which I give you only change the UI, however it will not affect any business logic. You need connect VBA module to handle your Issue.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 18, 2012 5:47 AM
    Moderator
  • Hi Tom,

    I'd like to custom the BackStage in office 2010, so my xml string like following code

    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
     <backstage>
        <tab idMso ="TabNew" visible ="false"></tab>
        <tab idMso ="TabInfo" visible ="false"></tab>
        <tab idMso ="TabRecent" visible ="false"></tab>
     </backstage>
    </customUI>
    However, It throws exception: The XML has invalid content and cannot be constructed as an element.
    Parameter name: outerXml, string xmlns="http://schemas.microsoft.com/office/2009/07/customui">.... is invalid, I have to use string xmlns="http://schemas.microsoft.com/office/2006/01/customui">...., But in this way, I cann't custom the backstage. Any way to slove it?
    I have tested OpenXMLSDKv2 and OpenXMLSDKv2.5, they all have this issue.
    • Edited by after_base Friday, December 21, 2012 6:22 AM
    Thursday, December 20, 2012 2:40 AM