none
word2013,ppt2013,excel2013:I want to disable the saveAs button according to the path -Open both local and server-side files RRS feed

  • Question

  • 上个问题的BUG

    Open alone can be disabled.

    Open both local and server-side files with bugs 

    first server file: disable save as is ok

    Do not close this file.Open the local file and find the BUG

    saveas cannot be disabled.

    How to solve it?thanks

    Code:

    ThisAddIn.cs

    public Microsoft.Office.Core.IRibbonExtensibility ribbon;

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                this.Application.WindowDeactivate += Application_WindowDeactivate1;

            }

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

            {

            }

            private void Application_WindowDeactivate1(Word.Document Doc, Word.Window Wn)

            {

                ((RibbonWord)ribbon).invalidateRibbon();

            }

            protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()

            {

                ribbon = new  RibbonWord();

                return ribbon;

            }

    Ribbon.cs

    public void invalidateRibbon()

            {

                this.ribbon.Invalidate();

            }

            #region//根据路径来禁用另存按钮

            public bool GetSaveEnabled(Office.IRibbonControl control)

            {

                //返回值

                bool disabled = true;

                //

                try

                {

                    //SP服务器的URL

                    string strDefaultLoc = ConfigurationManager.AppSettings["docPath"] == null ?

                        string.Empty : ConfigurationManager.AppSettings["docPath"].ToString();

                    Microsoft.Office.Interop.Word.Application app = Globals.ThisAddIn.Application;

                    //如果为空

                    if (app == null)

                    {

                        disabled = true;

                    }

                    else

                    {

                      

                        if (Globals.ThisAddIn.Application.Documents.Count > 0)

                        {

                            //得到路径

                            string paths = app.ActiveDocument == null ? string.Empty : app.ActiveDocument.FullName;

                            //缓存

                            if (paths.Contains(strDefaultLoc))

                            {

                                //返回false

                                disabled = false;

                            }

                            else

                            {

                                //复制给

                                disabled = true;

                            }

                        }

                      

                    }

                }

                catch (Exception ex)

                {

                    disabled= true;

                }

                //返回值

                return disabled;

            }

    The mouse switches back and forth with a BUG


    忘忧草,我的博客http://xiangzhangjun2006.blog.163.com

    Friday, November 17, 2017 8:56 AM

Answers

  • Hello,

    VSTO add-in is application level add-in, so the customization would be used for each document opened in the application. It is impossible to have no effect on each other.

    As you only want to disable the commands for different file path, you could use WindowActivate event instead to check the file Path of current document and refresh the ribbon using Ribbon.Invalidate() method.

    Regards,

    Celeste


    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.

    • Marked as answer by LOVE007 Monday, November 20, 2017 10:49 AM
    Monday, November 20, 2017 2:49 AM
    Moderator

All replies

  • Hello,

    When opening a 2nd document, the following event would fire 

    WindowDeactivate -> WindowActivate -> DocumentOpen

    So you could get the path of 1st document and 2nd document at these events and then decide to disable or enable the commands.

    //ThisAddIn.cs 
    public string firstDoc;
            public string secondDoc;
            private void Application_WindowDeactivate(Word.Document Doc, Word.Window Wn)
            {
                firstDoc = Doc.FullName;
            }
            private void Application_DocumentOpen(Word.Document Doc)
            {
                secondDoc = Doc.FullName;
                ((WordAddIn.Ribbon3)ribb).invalidateRibbon();
            }
    
    
    //Ribbon.cs
      public void invalidateRibbon()
            {
                this.ribbon.Invalidate();
            }
            public bool GetSaveEnabled(Office.IRibbonControl control)
            {
                bool disabled = true;
                if(Globals.ThisAddIn.Application.Documents.Count>0)
                {
                    if(Globals.ThisAddIn.firstDoc!=null && Globals.ThisAddIn.secondDoc != null)
                    {
                        MessageBox.Show(Globals.ThisAddIn.firstDoc + Globals.ThisAddIn.secondDoc);
                        // your code to disable or enable the command based on the two file path
                    }
    
                }
                return disabled;
            }

    Regards,

    Celeste


    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, November 17, 2017 9:53 AM
    Moderator
  • I'm sorry, maybe I'm not quite sure.

    My requirement is:

    1、open the server file separately to disable saveas

    2、open the local file separately can not disable saveas

    3、Open the server file and local file at the same time:

    the server file to disable saveas,the local file can not disable saveas, no influence on each other.

    thank you!


    忘忧草,我的博客http://xiangzhangjun2006.blog.163.com

    Saturday, November 18, 2017 2:21 AM
  • Hello,

    VSTO add-in is application level add-in, so the customization would be used for each document opened in the application. It is impossible to have no effect on each other.

    As you only want to disable the commands for different file path, you could use WindowActivate event instead to check the file Path of current document and refresh the ribbon using Ribbon.Invalidate() method.

    Regards,

    Celeste


    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.

    • Marked as answer by LOVE007 Monday, November 20, 2017 10:49 AM
    Monday, November 20, 2017 2:49 AM
    Moderator