Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Word 2007 Ribbon Customization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
klav - 28 Dec 2007 15:25 GMT
I am working on converting 2003 custom toolbars to ribbons. I am using the
custom UI editor to create a ribbon and using the callback function to call
my macros within my template.

I have several toolbars and want only certain ones to be displayed at
certain times. How do I accomplish this with a ribbon -- is it possible to
have multiple ribbons and display only certain ones at certain times? I would
also be OK with just graying out the tabs that are not applicable at specific
times but I don't know how to do this either. Any help would be greatly
apprecaited.
Greg Maxey - 30 Dec 2007 14:56 GMT
klav,

I am a novice in this area so take this simply as a working example but very
likely not the best solution.

The following XML creates a Tab "Practice Tab" with a "Practive Group" and a
single command button:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="Main.Onload">
<ribbon>
  <tabs>
     <tab id="myTab1" label="Practice Tab" getVisible="Main.getVisible">
         <group id="CntrGrp" label="Practice Group">
             <button id="myBtn1" getLabel="Main.getLable" size="normal"
getEnabled="Main.getEnabled" onAction="Main.SampleMacro"/>
         </group>
     </tab>
 </tabs>
</ribbon>
</customUI>

In this example, the tab should only show if the IP is within a table.  The
command button should be enabled if the table is uniform (the label should
be "Uniform") and disabled if the table is not uniform (the label should be
"Non-Uniform").

My understanding is that you need the "Onload" callback in order to create a
Ribbon obect in your VBA project.

As the ribbonX is loaded a Ribbon object is created in the project and the
initial state of the controls are determined based on the callbacks.  These
values are stored in a cache.

The callbacks are located in a document module named Main:

Option Explicit
Private myDynamicMenu As myClass1
Public myRibbon As IRibbonUI

Sub AutoOpen()
Set myDynamicMenu = New myClass1
End Sub

Sub OnLoad(Ribbon As IRibbonUI)
Set myRibbon = Ribbon
End Sub

Private Sub getEnabled(control As iRibbonControl, ByRef enabled)
If control.ID = "myBtn1" Then
 enabled = Selection.Tables(1).Uniform
End If
End Sub

Private Sub getLable(control As iRibbonControl, ByRef label)
If control.ID = "myBtn1" Then
 If Selection.Tables(1).Uniform Then
   label = "Uniform"
 Else
   label = "Non-Uniform"
 End If
End If

End Sub
Private Sub getVisible(control As iRibbonControl, ByRef visible)
If control.ID = "myTab1" Then
 visible = Selection.Information(wdWithInTable)
End If
End Sub

Sub SampleMacro(ByVal iRibbonControl)
MsgBox Selection.Tables(1).Columns.Count
End Sub

Now in order to change the values in the cache the controls must be
invalidated.  To do this in this example, I used a Class module
WindowSelectionChange event:

The project contains a Class module named myClass1:

Option Explicit
Private WithEvents mWordApp As Word.Application

Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub

Private Sub mWordApp_WindowSelectionChange(ByVal oSel As Selection)
myRibbon.InvalidateControl ("myTab1")
myRibbon.InvalidateControl ("myBtn1")
End Sub

Each time the userd moves the selection (i.e., clicks in or out of a table)
the event fires and invalidates the two controls (you can also invalide the
whole ribbon).  This forces Word to go out and look for the value of these
controls again.

I hope this helps.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> I am working on converting 2003 custom toolbars to ribbons. I am
> using the custom UI editor to create a ribbon and using the callback
[quoted text clipped - 6 lines]
> are not applicable at specific times but I don't know how to do this
> either. Any help would be greatly apprecaited.
klav - 31 Dec 2007 16:12 GMT
This is EXACTLY what I was looking for. Thank you so much.

> klav,
>
[quoted text clipped - 106 lines]
> > are not applicable at specific times but I don't know how to do this
> > either. Any help would be greatly apprecaited.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.