[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Toggle Word 2010 custom button - VBA Error 91

Posted on 2011-02-12
11
Medium Priority
?
1,416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Accepted Solution

by:
TommySzalapski earned 1500 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

656 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