none
NullReferenceException Occuring with Dynamic Ribbon Customisation RRS feed

  • Question

  • I am creating a 2007 MS Word Addin using Visual Studio Ultimate 2010 programming in visual basic .net 4.0.

    I have created a ribbon xml which a check box interacts with a custom task pane.

    I have created an onAction event and getPressed Function in the microsoft office word 2007 ribbon xml. I am programming in visual basic .Net 4.0

    When I click the checkbox the custom pane is visible and when the checkbox is clicked again it is invisible.

    The functionality I want to add is when the custom task pane is closed the checkbox is unticked.

    I have added the following code to THisAddin.vb

    Private myUserControl1 As UserControl1
    Private WithEvents myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane
    
    Dim r As Ribbon1
    
    Private Sub ThisAddIn_Startup() Handles Me.Startup
    
        Dim rUI As Microsoft.Office.Core.IRibbonUI = r.ribbon
    
        myUserControl1 = New UserControl1
        myCustomTaskPane = Me.CustomTaskPanes.Add(myUserControl1, "Picture Table Editor Pane")
    
        With myCustomTaskPane
            .DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating
            .Width = 300
            .Height = 400
            .DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionLeft
            .Width = 300
        End With
    
    End Sub
    
    Protected Overrides Function CreateRibbonExtensibilityObject() As Microsoft.Office.Core.IRibbonExtensibility
        Dim r As New Ribbon1
        'Return New Ribbon1()
        Return r
    End Function
    
    Private Sub myCustomTaskPane_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myCustomTaskPane.VisibleChanged
        r.ribbon.InvalidateControl("Checkbox1" = False)
    End Sub
    
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
    
    End Sub
    
    End Class

    I have also made the class Ribbon1.vb public

    However when I debug the solution I get a NullReferenceException

    How do I get around this and get the checkbox to untick when the custom task pane closes.

    Thanks

    Thursday, September 26, 2013 5:39 AM

Answers

  • Thanks Cindy

    I changed the getpressed function back. Still get the nullreferenceexception of the invalidatecontrol

    So how do I set the value of the checkbox to the visible value of the custom task pane when the invalidatecontrol is executed.

    • Marked as answer by tfitzhardinge Tuesday, October 15, 2013 10:03 AM
    Saturday, October 5, 2013 10:57 PM

All replies

  • Which line of code is triggering the exception? When you hover the mouse pointer over the objects in that line of code, which one is "Nothing" instead of offering a list of available properties?

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 1, 2013 6:14 PM
    Moderator
  • Hi Cindy

    The statement which contains the InvalidateControl is throwing the Null reference exception

    r.ribbon.InvalidateControl("Checkbox1" = False)

    I have never used this statement so I don't know if I have used it correctly.

    Also how does the InvalidateControl work. Does it execute the Ribbon Load event again or does it execute the callbacks (ie getPressed or onAction).

    Wednesday, October 2, 2013 2:43 AM
  • InvalidateControl triggers the callbacks (except onAction, just the "get" ones) for the control you specify.

    You should not be setting the control you want to invalidate to any kind of value, you pass only the control ID. The "get" callbacks take care of determining the value. You just have to make sure you've stored the value somewhere and that getPressed looks that up.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 2, 2013 2:04 PM
    Moderator
  • Thanks Cindy

    I have changed the following with the get pressed event to word with the invalidate control in ribbon.vb which I am assuming is correct

        Public Function GetPressed(ByVal control As Office.IRibbonControl, ByVal value As Boolean)

            value = Globals.ThisAddIn.CustomTaskPanes(0).Visible

            Return Globals.ThisAddIn.CustomTaskPanes(0).Visible

        End Function

    and changed the custom task pane VisibleChanged event

    Private Sub myCustomTaskPane_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myCustomTaskPane.VisibleChanged
                    r.ribbon.InvalidateControl("CheckBox1")

        End Sub

    However I am getting a null reference exception within the This_Startup Event:

    Dim rUI As Office.IRibbonUI = r.ribbon

    (a direct C# conversion to vb.net from 

    Office.IRibbonUI rUI = r.ribbon;)

    It asks for the keyword New which produces a syntax error. 

    Thursday, October 3, 2013 12:27 PM
  • As I said before, as far as I can tell, that line of code does not belong in ThisAddin_Startup.

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 3, 2013 3:45 PM
    Moderator
  • Does my GetPressed event look right so that the invalidatecontrol works or do I need to add something?

    Public Function GetPressed(ByVal control As Office.IRibbonControl, ByVal value As Boolean)

            value = Globals.ThisAddIn.CustomTaskPanes(0).Visible

            Return Globals.ThisAddIn.CustomTaskPanes(0).Visible

        End Function

    Friday, October 4, 2013 7:06 AM
  • Well, if I look in Part 3 of the "Customization" article (http://msdn.microsoft.com/en-us/library/aa722523(v=office.12)) the method signature for the getPressed method of a checkbox has only one parameter in VB.NET: control as IRibbonControl. It doesn't have a second "value" parameter.

    So I'd say you need to fix that and then you'd use only the line that starts with Return.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 4, 2013 2:47 PM
    Moderator
  • Thanks Cindy

    I changed the getpressed function back. Still get the nullreferenceexception of the invalidatecontrol

    So how do I set the value of the checkbox to the visible value of the custom task pane when the invalidatecontrol is executed.

    • Marked as answer by tfitzhardinge Tuesday, October 15, 2013 10:03 AM
    Saturday, October 5, 2013 10:57 PM