none
2010 Word Add-in does not work in a particular case. RRS feed

  • Question

  • I am just starting out with a Word VSTO Add-in. I want to add a group to the ribbon,

    which has a button to toggle a custom task pane. I want each document to have it's own independent task pane.

    I have this mostly working, but there is one situation that doesn't work:

    Start Word - new document opened, all works fine
    Open existing document (closes empty document)
    Click on toggle button, pane doesn't appear
    Create new document or open ANOTHER existing document, pane appears on that document
    Pane now works as expected on all documents, including the problem one from 2/3.
    Note that if you type something into the new document (1), everything works as expected,

    so this seems to be something to do with the existing document loading over the top of the initial empty one, 
    but I can't work out what's going on.

    Here's my code from ThisAddIn class:

    Note that the PaneControl is a totally empty User Control, behaviour doesn't change when I add stuff to it.

    public partial class ThisAddIn
    {
        private CustomTaskPane CurrentTaskPane(Object window)
        {
            foreach (CustomTaskPane ctp in CustomTaskPanes)
            {
                if (ctp.Window.Equals(window))
                {
                    return ctp;
                }
            }
            return null;
        }
        public bool ToggleTaskPane(Object window)
        {
            CustomTaskPane ctp = CurrentTaskPane(window);
            if (ctp != null)
            {
                ctp.Visible = !ctp.Visible;
                return ctp.Visible;
            }
            else
            {
                return false;
            }
        }
        private void RemoveOrphanedTaskPanes()
        {
            for (int i = CustomTaskPanes.Count; i > 0; i--)
            {
                var ctp = CustomTaskPanes[i - 1];
                if (ctp.Window == null)
                {
                    CustomTaskPanes.Remove(ctp);
                }
            }
        }
        private void CreateTaskPane(Object window)
        {
            try
            {
                RemoveOrphanedTaskPanes();
                // Add the new one
                PaneControl ucPaneControl = new PaneControl();
                CustomTaskPane ctp = CustomTaskPanes.Add(ucPaneControl, "Test Pane", window);
                ctp.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
                ctp.Width = 300;
            }
            catch
            {
                MessageBox.Show("Unable to create pane");
            }
        }

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            try
            {
                Word.ApplicationEvents4_Event app = (Word.ApplicationEvents4_Event)Application;


     // Disambiguate
                app.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
                app.NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
                app.DocumentChange += new Word.ApplicationEvents4_DocumentChangeEventHandler(Application_DocumentChange);
                CreateTaskPane(Application.ActiveWindow);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            RemoveOrphanedTaskPanes();
        }
        void Application_DocumentChange()
        {
            RemoveOrphanedTaskPanes();
        }
        void Application_DocumentOpen(Word.Document Doc)
        {
            // Creeate pane for existing document
            CreateTaskPane(Doc.ActiveWindow);
        }
        void Application_NewDocument(Word.Document Doc)
        {
            // Creeate pane for new blank document
            CreateTaskPane(Doc.ActiveWindow);
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion

    Ribbon class code:

     Globals.ThisAddIn.ToggleTaskPane(Globals.ThisAddIn.Application.ActiveWindow);















    Friday, July 25, 2014 4:43 PM

Answers

  • Hi,

    As far as I know, the relationship between custom Task Pane and window is 1 to 1. If you just create a custom task pane with the code below in ThisAddin.cs, and then open an existing document or create a blank new one, you can test to find that, each document has its own independent task pane. And if you have opened multiple document windows, closing a task pane of one window will not affect the displaying of others.

    private PaneControl ucPaneControl;
    private Microsoft.Office.Tools.CustomTaskPane ctp;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        ucPaneControl = new PaneControl();
        ctp = this.CustomTaskPanes.Add(ucPaneControl, "Test Pane");
        ctp.Visible = true;
    }

    If you want to use a toggle button to hide/show the custom task pane for independent document windows, you could refer to the link below to use the toggle button in a correct way. After testing, the code introduced in it works well in my Word add-in.

    Walkthrough: Synchronizing a Custom Task Pane with a Ribbon Button

    By the way, if you have create a custom Task pane in ThisAddIn_Startup function, you don't need to call the CurrentTaskPane function in the document open, new and change event.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 28, 2014 5:56 AM
    Moderator
  • Hi,

    I suspect you mean the "task bar" as the title of document as the picture below.

    If I have no misunderstanding, I think the issue is by design and has nothing to do with your original post.

    In Word, when you create a blank document and then open an existing document by File->Open, the existing document will replace the blank document automatically. Unless you write some text in blank document, Word will keep all the text in the blank document and open the existing document in a new windows.

    For more question about end-user feature of Word product, I suggest you posting in Word IT pro Forum for more effective responses.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by .Net User Wednesday, July 30, 2014 9:50 AM
    Wednesday, July 30, 2014 9:26 AM
    Moderator

All replies

  • Hi ,

    I am working on word add-in.I have a problem in custom task pane for Word 2010 which is supposed to work on multiple document windows.If I open multiple document windows, the custom task pane seems to work common for all open windows.That is when I click the toggle button to hide the custom task pane, it hides the custom task pane on all the open windows.How would i make  the custom task pane work independently for each window.window. Coud you please help me.

    Below is my code.

    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Xml;
    using System.Xml.Linq;
    using Microsoft.Office.Tools;
    using Microsoft.Office.Tools.Ribbon;
    using WordAddIn1.Properties;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;

    namespace WordAddIn1
    {

        public partial class ThisAddIn
        {
          
            private bool _initialized = false;
            private TaskPane _myUserControl;
            public CustomTaskPane TaskPane;
           
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {


                ShowCustomPane();
               

                Debug.Assert(_initialized);
           
            }

            private void ShowCustomPane()
            {
             _myUserControl = new TaskPane();
                TaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(_myUserControl, "BookMarks List");
                TaskPane.DockPosition =
                Office.MsoCTPDockPosition.msoCTPDockPositionRight;
                TaskPane.Width = 350;
             
            }
          
            private void InitializeCustom()
            {
                
                _initialized = true;
                Globals.ThisAddIn.Application.DocumentOpen +=
                    new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_open);

                ((Word.ApplicationEvents4_Event)this.Application).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);


                       
               

            }
           
            
            private void Application_open(Word.Document document)
            {
               
                         ShowCustomPane();
                

            }

           
            
            private void Application_NewDocument(Word.Document document)
            {
                
             ShowCustomPane();

            }
           

            private void Application_WindowActivate(Word.Document document,Word.Window wn)
            {
                
               // var myTaskPane = this.CustomTaskPanes.Add(taskPaneView, "BookMarks");
                //XDocument doc = XDocument.Load(@"C:\Projects\Learning\Projdoc.xml");
                //taskPaneView.CustomXMLParts.Add(doc.ToString(), missing);
              //document.CustomXMLParts.

                //myTaskPane.Visible = true;
               // string connectionString = Properties.Settings.Default.DB;

               //// string connectionString = ConfigurationManager.ConnectionStrings["MVCLearning"].ConnectionString;
               // SqlConnection con = new SqlConnection(connectionString);
               // string sql = "select * from BookMarks";
               // SqlDataAdapter da = new SqlDataAdapter(sql, con);

               // DataSet ds = new DataSet("BookMarks");
               // da.Fill(ds);
               // string strXML = ds.GetXml();
               // XmlDocument doc = new XmlDocument();
               // doc.LoadXml(strXML);


                XDocument doc = XDocument.Load(@"C:\Projects\Learning\Projdoc.xml");
                document.CustomXMLParts.Add(doc.ToString(), missing);

            }
                   
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
                //TaskPane = null;

            }

            #region VSTO generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
              
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }

    Friday, July 25, 2014 8:59 AM
  • Hi 

    I have implemented using suggested link.ToggleButton status is not updating propery and when i click on toggle button adding task panes in all open windows and next click duplicating the task panes on open windows.Below is my code.

                                                                 

    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.Linq;
    using Microsoft.Office.Tools;
    using Microsoft.Office.Tools.Ribbon;
    using WordAddIn1.Properties;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;

    namespace WordAddIn1
    {

        public partial class ThisAddIn
        {

            private bool _initialized = false;
            private TaskPane _myUserControl;
            public Microsoft.Office.Tools.CustomTaskPane taskPaneValue;
            public bool calendarDisplayed, showCalendar;

            public Microsoft.Office.Tools.CustomTaskPane TaskPane
            {
                get
                {
                    return taskPaneValue;
                }
            }

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Globals.ThisAddIn.Application.DocumentOpen +=
                 new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);

                ((Word.ApplicationEvents4_Event)this.Application).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);


            }


            public void AddAllTreevieTaskPanes()
            {
                if (Globals.ThisAddIn.Application.Documents.Count > 0)
                {
                    if ((this.Application.ShowWindowsInTaskbar == true))
                    {
                        foreach (Word.Document _doc  in this.Application.Documents)
                        {
                            AllTreeviewPane(_doc);

                        }
                    }
                    else
                    {
                        if (!calendarDisplayed)
                        {
                            AllTreeviewPane(this.Application.ActiveDocument);
                        }
                    }
                    calendarDisplayed = true;
                    }
                }

            public void AllTreeviewPane(Word.Document doc)
            {
                _myUserControl= new TaskPane();
                taskPaneValue = this.CustomTaskPanes.Add(_myUserControl, "BookMark List", doc.ActiveWindow);
                taskPaneValue.Visible = true;
            }


            public void RemoveAllTreeviewTaskPanes()
            {
                for (int i = this.CustomTaskPanes.Count; i > 0; i--)
                {
                    taskPaneValue = this.CustomTaskPanes[i - 1];
                    if (taskPaneValue.Title == "BookMark List")
                    {
                        this.CustomTaskPanes.RemoveAt(i - 1);
                    }
                }
            }


            private void Application_NewDocument(Word.Document Doc)
            {
                if (showCalendar && this.Application.ShowWindowsInTaskbar)
                {
                    AllTreeviewPane(Doc);
                }
            }

            private void Application_DocumentOpen(Word.Document Doc)
            {
                if (showCalendar && this.Application.ShowWindowsInTaskbar)
                {
                    AllTreeviewPane(Doc);
                }
            }

            private void RemoveOrphanedTaskPanes()
            {
                for (int i = this.CustomTaskPanes.Count; i > 0; i--)
                {
                    taskPaneValue = this.CustomTaskPanes[i - 1];
                    if (taskPaneValue.Window == null)
                    {
                        this.CustomTaskPanes.Remove(taskPaneValue);
                    }
                }
            }
            private void Application_DocumentChange()
            {
                RemoveOrphanedTaskPanes();
            }



            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
                //TaskPane = null;

            }

            #region VSTO generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {

                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }

            #endregion
        }
    }

    ---Ribbon Class

              

      private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
            {
                //Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
                //ManageTaskPaneRibbon riToggleButton1 = Globals.Ribbons.ManageTaskPaneRibbon;
                if (Globals.ThisAddIn.showCalendar==false)
                {
                    Globals.ThisAddIn.AddAllTreevieTaskPanes();
                }
                else
                {
                    Globals.ThisAddIn.RemoveAllTreeviewTaskPanes();
                }

            }

    Please help me

    Friday, July 25, 2014 3:09 PM
  • Did you try to debug the code? Is the DocumentOpen event fired?
    Saturday, July 26, 2014 10:45 AM
  • yes.DocumentOpen event is fired but new document event is not fired.
    • Edited by .Net User Saturday, July 26, 2014 9:02 PM
    Saturday, July 26, 2014 8:55 PM
  • Hi,

    As far as I know, the relationship between custom Task Pane and window is 1 to 1. If you just create a custom task pane with the code below in ThisAddin.cs, and then open an existing document or create a blank new one, you can test to find that, each document has its own independent task pane. And if you have opened multiple document windows, closing a task pane of one window will not affect the displaying of others.

    private PaneControl ucPaneControl;
    private Microsoft.Office.Tools.CustomTaskPane ctp;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        ucPaneControl = new PaneControl();
        ctp = this.CustomTaskPanes.Add(ucPaneControl, "Test Pane");
        ctp.Visible = true;
    }

    If you want to use a toggle button to hide/show the custom task pane for independent document windows, you could refer to the link below to use the toggle button in a correct way. After testing, the code introduced in it works well in my Word add-in.

    Walkthrough: Synchronizing a Custom Task Pane with a Ribbon Button

    By the way, if you have create a custom Task pane in ThisAddIn_Startup function, you don't need to call the CurrentTaskPane function in the document open, new and change event.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 28, 2014 5:56 AM
    Moderator
  • Dear All,

    The above code is working in all scenarios  except one.

    1.If  i open the existing document(test.doc) after  the new blank document(document1,document2),the document2 is closing and i can see document1,test.doc and document2 is disappears.

    2.I can see document1,document2,test.doc only if i write some text in blank document before i open the existing document.I was unable to find out the issue.


    Monday, July 28, 2014 9:43 AM
  • Hi,

    What's the meaning of "i can see document1,test.doc and document2 is disappears"? Do you mean the custom task pane disappears? Which code are you using for the special scenario?

    After testing, I cannot reproduce your issue.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 29, 2014 9:08 AM
    Moderator
  • Thanks for testing my code.

    Could you please follow the below steps to reproduce the issue.

    1.Double click the microsoft word 2010 from start button.It opens document1 and again do the same opens document2 likewise open 3 documents

    2.Now double click on the any save document.

    3.On task bar we have opened total 4 documents(document1,document2,document3,saved document.

    4.The issue was only we can see on the task bar 3 word documents(document1,document2,saved document).

    5.Document3 is disspears.

    Wednesday, July 30, 2014 8:52 AM
  • Hi,

    I suspect you mean the "task bar" as the title of document as the picture below.

    If I have no misunderstanding, I think the issue is by design and has nothing to do with your original post.

    In Word, when you create a blank document and then open an existing document by File->Open, the existing document will replace the blank document automatically. Unless you write some text in blank document, Word will keep all the text in the blank document and open the existing document in a new windows.

    For more question about end-user feature of Word product, I suggest you posting in Word IT pro Forum for more effective responses.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by .Net User Wednesday, July 30, 2014 9:50 AM
    Wednesday, July 30, 2014 9:26 AM
    Moderator
  • Hi Luna Zhang,

    Thank you very much for your reply.I don't know that Microsoft word works in that way and thought this is the issue with my code.

    Thanks alot.


    • Edited by .Net User Wednesday, July 30, 2014 9:51 AM
    Wednesday, July 30, 2014 9:50 AM