Disabling custom buttons on ribbon unless file is open

E

EKH

Hi

In Word 2007, I've customized the ribbon for macros that are saved to a
..dotm in the StartUp folder. These, by default, are "enabled" so that user
can click them even when no Word document is open. Of course, this result in
a runtime error. How do I disable these when no document is open as is the
default for the built-in commands on the ribbon?
 
G

Gordon Bentley-Mix on news.microsoft.com

While I'm sure this can be done, there is a quick-n-dirty workaround that
might be easier to implement. In the macros called from the Ribbon include a
check on Application.Documents.Count and if it's less than 1, display a
warning message; otherwise run the macro.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
E

EKH

Great idea! I did the following so the user won't even have to respond to a
warning:

If Documents.Count = 0 Then
Application.Documents.Add
End If

I'm still curious about disabling the ribbon when there's no active
document, but I'm happy to have this workaround.

Thanks much!
 
G

Greg Maxey

EKH,

I haven't proofed or tested, but I would probably start by adding a Class
module to the template to monitor the DocumentChange event. This would tell
you if the documentment count was 0 or > 0 any time a document was opened or
closed. You could then use this event to to invalidate your ribbon. When
the ribbon revalidated you could enable or disable your controls based on
the document.count.

Great idea! I did the following so the user won't even have to
 
G

Greg Maxey

EKH,

It seems to work (I have done very limited testing).

In the template create a Class module named "appClass" and paste in this
code:

Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_DocumentChange()
bEnable = False
myRibbon.Invalidate
If Documents.Count > 0 Then bEnable = True
End Sub

In your ribbon control project module (I call my RibCon) paste in this code:

Option Explicit
Public bEnable As Boolean
Public myRibbon As IRibbonUI

Sub OnLoad(ribbon As IRibbonUI)
'Create a ribbon instance
Set myRibbon = ribbon
End Sub

Sub BtnOnAction(control As IRibbonControl)
MsgBox control.ID & " depressed"
End Sub

Sub GetEnabled(control As IRibbonControl, ByRef returnedVal)
If bEnable Then returnedVal = "True"
End Sub

In a standard project module paste in this code:

Option Explicit
Dim oAppClass As New appClass
Sub AutoExec()
Set oAppClass.oApp = Word.Application
End Sub

For the test I created a simple ribbon interface template and stored it in
the Word Startup folder. Here is the RibbonX for the test file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="RibCon.Onload">
<ribbon>
<tabs>
<tab id="TabTest" label="Test Tab">
<group id="Grp1" label="Test Group">
<button id="Btn1" label="Button 1" getEnabled="RibCon.GetEnabled"
onAction="RibCon.BtnOnAction"/>
<button id="Btn2" label="Button 2" onAction="RibCon.BtnOnAction"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
 
G

Greg Maxey

Gordon,

Even quicker and perhaps not as dirty would be to include the condition in
the VBA callback that can be used for multiple controls:

Sub BtnOnAction(control As IRibbonControl)
If Documents.Count = 0 Then Exit Sub
Select Case control.ID
Case "Btn1"
MsgBox "Call this macro"
Case "Btn2"
MsgBox "Call that macro"
End Select
End Sub
 
E

EKH

Hi Greg,

I'm new at VBA so it took me a while to get this going, but I did manage to
apply your code for monitoring the document change event to my ribbon
customization. That does the trick!

Thanks much!
 
G

Greg Maxey

Hi Greg,

I'm new at VBA so it took me a while to get this going, but I did manage to
apply your code for monitoring the document change event to my ribbon
customization.  That does the trick!

Thanks much!  








- Show quoted text -

You're welcome. Glad I could help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top