none
Word CustomTaskPane + Ribbon RRS feed

  • Question

  • Hi, 

    I have created a task pane to insert stuff into the current document in Word, using a Word Add-In project. It worked wonderfully, until I opened another Window (it could become a black rectangle, and solutions I found in forums didn't work). The only thing I could do without adding a custom ribbon was to make the last opened window to "acquire" the plugin (actually, closing and reinstanciating) but it is pretty messy.

    So I added 2 buttons to the ribbon, as in a tutorial I found on Microsoft website. Just: it worked for Excel and I had to adapt it. Here is my code:

    Public Class MyRibbon
    
        Dim actionPane As New MyActionsPaneControl()
        Dim taskPane As Microsoft.Office.Tools.CustomTaskPane
    
        Private Sub MyRibbon_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    
            taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(actionPane, "My Title")
    
            taskPane.Width = 486
    
            actionPane.Hide()
            taskPane.Visible = False
    
            ToggleButton1.Enabled = False
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
            Try
                actionPane.Show()
                actionPane.Recharger()
                taskPane.Visible = True
                ToggleButton1.Enabled = True
                ToggleButton1.Checked = True
                Button1.Enabled = False
            Catch ex As Exception
                MessageBox.Show("Following error occured: " & ex.Message & vbNewLine & "Details : " & vbNewLine & ex.StackTrace)
            End Try
        End Sub
    
        Private Sub ToggleButton1_Click(sender As Object, e As RibbonControlEventArgs) Handles ToggleButton1.Click
            Try
                actionPane.Hide()
                taskPane.Visible = False
                ToggleButton1.Enabled = False
                ToggleButton1.Checked = False
                Button1.Enabled = True
            Catch ex As Exception
                MessageBox.Show("Following error occured : " & ex.Message & vbNewLine & "Details : " & vbNewLine & ex.StackTrace)
            End Try
        End Sub
    End Class

    Button1 is "open the pane", ToggleButton1 is "close the pane".

    It works well, until I open a second window. Then, what buttons do applies only to the first window. 

    I've seen that Globals.ThisAddIn.CustomTaskPanes.Add has a parameter of type "Window" and I think it may be the one I would have to provide but no variable of this type seems to be available AND initialized at the time of the call. 

    Could anyone help me with this issue ?

    Thank you in advance for any help.

    Friday, February 9, 2018 9:03 AM

All replies

  • Hello,

    Word can have multiple document windows opened for the single process (an instance of the Application class). Here is what MSDN states for that:

    Custom task panes are associated with a document frame window, which presents a view of a document or item to the user. The task pane is visible only when the associated window is visible.

    To determine which window displays the custom task pane, use the appropriate Add method overload when you create the task pane:

    Some Office applications require explicit instructions for when to create or display your task pane when more than one window is open. This makes it important to consider where to instantiate the custom task pane in your code to ensure that the task pane appears with the appropriate documents or items in the application. For more information, see Managing Custom Task Panes in Application Windows.

    For example, you can handle the DocumentOpen event of the Application class where you can add a task pane: 

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    Application.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
    }
    
    void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
    {
    myUserControl1 = new UserControl1();
    myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "MyTask Pane", docWindow);
    
    myCustomTaskPane.Width = 300;
    myCustomTaskPane.Visible = true;
    }

    To monitor the state of document windows in Word, you can handle the following events:


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, February 12, 2018 1:01 AM
  • Hi pocky6,

    You had mentioned that,"I have created a task pane to insert stuff into the current document in Word, using a Word Add-In project. It worked wonderfully, until I opened another Window (it could become a black rectangle,"

    I try to create Add in on my side and try to reproduce the issue.

    I find that it works correctly and task pane not turn in to black rectangle.

    Here, i want to confirm with you On which version of Word you are developing this task pane?

    I made this test on Word 2016.

    You can try to make a test on different version of Word and find the difference in the behavior of the task pane.

    Then you can try to develop a robust add in which can work for all the version of Word without having any issue.

    Regards

    Deepak


    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.

    Monday, February 12, 2018 5:24 AM
    Moderator