• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1423
  • Last Modified:

Toggle Word 2010 custom button - VBA Error 91

Hi Experts

I am trying to create a toggle a custom control on a Word 2010 custom ribbon.  When I click the button I get Error 91'Object variable or With block variable not set' on the following line of code:

RibbonUI.InvalidateControl "FormFields_Btn6"

Both the VBA code  and the relevant XML are below.

Please can you tell me where I am going wrong and how to put it right?

Many thanks.
<customUI
	onLoad="rxIRibbonUI_onLoad_LK1"
	xmlns="http://schemas.microsoft.com/office/2009/07/customui">

Open in new window

Option Explicit

Private RibbonUI As IRibbonUI

Private Sub rxIRibbonUI_onLoad_LK1(ribbon As IRibbonUI)
    Set RibbonUI = ribbon
End Sub

Private Sub ProtectForm(control As IRibbonControl, Pressed As Boolean)
    If Documents.Count > 0 Then
        If Pressed Then
            If ActiveDocument.ProtectionType = wdNoProtection Then
                ActiveDocument.Protect wdAllowOnlyFormFields
            End If
        Else
            ActiveDocument.Unprotect
        End If
    End If
    RibbonUI.InvalidateControl "FormFields_Btn6"
End Sub

Public Sub FormFields_Btn6_getPressed(control As IRibbonControl, ByRef returnedVal)
    If Documents.Count > 0 Then
        returnedVal = (Not ActiveDocument.ProtectionType = wdNoProtection)
    End If
End Sub

Public Sub FormFields_Btn6_getImage(control As IRibbonControl, ByRef returnedVal)
    returnedVal = "Lock"
End Sub

Open in new window

0
MonteDelMar
Asked:
MonteDelMar
  • 6
  • 5
1 Solution
 
TommySzalapskiCommented:
The documentation for VBA shows you actually need the () around the parameter. Have you tried that yet?
RibbonUI.InvalidateControl("FormFields_Btn6")
Source: http://msdn.microsoft.com/en-us/library/aa433553(v=office.12).aspx
0
 
TommySzalapskiCommented:
If the above doesn't work then your rxIRibbonUI_onLoad_LK1 must not be getting called.
Try something like
If RibbonUI Is Nothing Then
    MsgBox "Error: OnLoad didn't run."
Else
    RibbonUI.InvalidateControl("FormFields_Btn6")
End If

Open in new window

0
 
MonteDelMarAuthor Commented:
Thanks for your reply, Tommy.

I had tried the brackets around the ControlID but that did not make any difference.  However, I replaced the line

RibbonUI.InvalidateControl "FormFields_Btn6"

in my code with your suggested

If RibbonUI Is Nothing Then
    MsgBox "Error: OnLoad didn't run."
Else
    RibbonUI.InvalidateControl("FormFields_Btn6")
End If

and you are right - OnLoad did not run.

Any ideas as to why that would be?
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
TommySzalapskiCommented:
It looks like the names match properly, that's good.
You could put
MsgBox "Loading..."
in the loading function to make absolutely sure that it isn't running. (It's also possible that it is running, but there's an issue with a variable).
Exactly how did you set the whole thing up? (i.e. where did you put your xml and how are you connecting it etc.)
0
 
MonteDelMarAuthor Commented:
I tried before & after message boxes and it is running.

The xml code is in a template (.dotm) that is stored in the Startup folder.  Does that answer the question?
0
 
TommySzalapskiCommented:
So the rxIRibbonUI_onLoad_LK1 function is actually running, but when you run ProtectForm RibbonUI is nothing?

In the load function is ribbon nothing?

The only difference I see between yours and the sample from MS is that their global variable is Public and yours is Private.

I'll have more direct access to a computer with 2007/2010 in a few hours so I can try some tests to see if I can replicate and subsequently solve the issue. It might help if I could see the rest of the xml or at least a minimal sample that demonstrates the issue.
0
 
MonteDelMarAuthor Commented:
Thank you.  I will send you a sample between now and then.
0
 
MonteDelMarAuthor Commented:
Well what do you know?  It's working now.  

Would you mind if I left this question open for a short while in case it comes back?
0
 
TommySzalapskiCommented:
If you hadn't restarted the right thing after adding the template to the startup then that may have caused it. Who knows? Sometimes computers just randomly start working too. I've fixed close to a dozen computers in my short IT career by just walking up to them. The Sears guy does the same thing with my washing machine.
0
 
MonteDelMarAuthor Commented:
:-D
0
 
MonteDelMarAuthor Commented:
Although the problem 'went away', the Expert answer pointed me in the right direction to establish what was happening in my code to cause the error when it did appear.  I think that it is possibly caused if a debug error occurs even if it is nothing to do with that function.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now