Link to home
Create AccountLog in
Avatar of MonteDelMar
MonteDelMar

asked on

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

Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

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
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

Avatar of MonteDelMar
MonteDelMar

ASKER

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?
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.)
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?
ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you.  I will send you a sample between now and then.
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?
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.
:-D
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.