locked
Declaration Public\Private and scope RRS feed

  • Question

  • I am trying to create a Custom Task Pane for a Word application level add-in. It followed the basic instructions fount here:

    http://msdn.microsoft.com/en-us/library/aa942846.aspx When I run the code the custom task pane is displayed.

    'These two declarations are required for the custom task pane.
        Private myUserControl1 As myUserControl
        Private myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            wApp = Globals.ThisAddIn.Application
            'These lines initialize the custom task pane
            myUserControl1 = New myUserControl
            myCustomTaskPane = Me.CustomTaskPanes.Add(myUserControl1, "Demo Task Pane")
            myCustomTaskPane.Visible = True
        End Sub

    I don't want the task pane to show on startup so I stetted out 'myCustomTaskPane.Visible = True I the procedure above and added it to a ribbon button click callback procedure shown below and after so fiddling, I got it to work.

    The first thing I did was simply add the line:

    'myCustomTaskPane.Visible = True

    The error list indicated "myCustomTaskPane is not declared.  It may be inaccessible due to the its protection level.

    Next I changed the declaration of "myCustomTaskPane" in the code above from Private to Public.  I thought that would make is accessible to all other code modules in the project.  The error list still reported the same issue.

    Finally I used Globals.ThisAddIn.CustomTaskPanes(0).Visible = True which worked.

    Public Sub RibbonButtonOnAction(ByVal control As Office.IRibbonControl) Select Case control.Id Case "btnTest1" MsgBox("Basic Test") Case "btnTest2" Globals.ThisAddIn.IntermediateTest() Case "btnTest3" 'myCustomTaskPane.Visible = True Globals.ThisAddIn.CustomTaskPanes(0).Visible = True End Select End Sub

    I am very new at all of this and even though I've been reading about VB until my eyes are bleary and just not sure what the proper approach would be.  I could live with using the index but I could see if I had dozens of task panes in a project then it might be difficult to keep track of them.  I've left the declarations in the ThisAddin module because that is what the article said to do.  Should I move them to the RibbonXML.vb module?  Why didn't declaring the variable as Public work?  Is there something else I could or should have done to ensure I could refer to the taskpane by name in my ribbon callback?

    Thanks.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Monday, November 24, 2014 6:48 PM

Answers

  • I can't see an explanation for this. The documentation is clear that the Add method returns the object that you need to work with, and the scope of the variable you are using would seem to be appropriate.  The Add method creates the wrapper, so you can't use MyUserControl1, but the purpose of returning the CustomTaskPane is to give you an object you can use. Perhaps the message is not accurate, and it's simply saying that the object can't be used like that.  Did you try

            Globals.ThisAddIn.CustomTaskPanes("Demo Task Pane").Visible = True

    At least that would make keeping track of them simpler. The index would be unusable of you had multiple adds and removes.

    • Marked as answer by Carl Cai Monday, December 8, 2014 5:27 AM
    Monday, November 24, 2014 9:25 PM