Solved

Toggle Word 2010 custom button - VBA Error 91

Posted on 2011-02-12
11
1,379 Views
Last Modified: 2012-05-11
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
Comment
Question by:MonteDelMar
  • 6
  • 5
11 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34879683
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34879697
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
 

Author Comment

by:MonteDelMar
ID: 34882663
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34885615
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
 

Author Comment

by:MonteDelMar
ID: 34886154
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 34886276
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
 

Author Comment

by:MonteDelMar
ID: 34886393
Thank you.  I will send you a sample between now and then.
0
 

Author Comment

by:MonteDelMar
ID: 34888806
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34889542
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
 

Author Comment

by:MonteDelMar
ID: 34889589
:-D
0
 

Author Closing Comment

by:MonteDelMar
ID: 34977337
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now