PDA

View Full Version : How can I get list of commands (and its ID) from the ribbon and groups (Word 2007)?



akokin
03-06-2008, 05:16 AM
How can I get list of commands (and its ID) from the ribbon and groups (in the Word 2007)? These commands miss into the list of commands
for Toolbars (if use one macro for building table of commands), e.g. Paste Special (or like PasteSpecialDialog).
Thank you very much!

TonyJollans
03-06-2008, 05:23 PM
I'm not sure i fully understand what you are asking. Ribbon commands do not, as far as I know, relate to toolbar commands in any useful way.

akokin
03-06-2008, 09:39 PM
Sorry.
There is some code:
Set oBtn = oPopUp.Controls.Add(msoControlButton, 1589)
Question: how can I get list of commands and relevant the commands ID
(identification numbers) from Word 2007?
If use macro (show below), than will building the table with list of the commands for tollbars. But it not have some commands, e.g. "Special Paste" that is on the "Home" tab (in the "Clipboard" group).
I need it. Thank you.
That code (many thanks Jean-Guy Marcil):



Option Explicit

Sub GetAllControlID()

Dim toolbarAll As CommandBar
Dim ctrAll As CommandBarControl
Dim colID As Collection
Dim colCap As Collection
Dim i As Long
Dim rgeDoc As Range

Const strLabel As String = "Toolbar Name:"
Set rgeDoc = ActiveDocument.Range
rgeDoc.Collapse wdCollapseStart

Set colID = New Collection
Set colCap = New Collection

For Each toolbarAll In Application.CommandBars
colID.Add strLabel
colCap.Add toolbarAll.Name
For Each ctrAll In toolbarAll.Controls
colID.Add ctrAll.ID
colCap.Add ctrAll.Caption
Next
Next

For i = 1 To colID.Count
With rgeDoc
.InsertAfter colID(i) & vbTab
.InsertAfter colCap(i) & vbCrLf
If colID(i) = strLabel Then
With .Paragraphs(.Paragraphs.Count).Range
With .Font
.Bold = True
.Size = 14
End With
With .ParagraphFormat
.SpaceBefore = 12
.SpaceAfter = 3
.KeepWithNext = True
End With
End With
End If
End With
Next

rgeDoc.ConvertToTable vbTab
End Sub
Further to my post:
I wrote automacro in the Word 2003 that add the command "Paste Special". Then I see code and recognized it ID - 755. But how can I get all these ID. That is the question! Thank you.

Nelviticus
03-07-2008, 05:09 AM
Is this any use to you?

2007 Office System Document: Lists of Control IDs
http://www.microsoft.com/downloads/details.aspx?familyid=4329D9E9-4D11-46A5-898D-23E4F331E9AE&displaylang=en

TonyJollans
03-07-2008, 05:14 AM
Now I understand. This has nothing to do with Word 2007, and, unfortunately, it is not entirely straightforward either.

When you iterate over the CommandBars collection you do not get all the CommandBars. In particular, you do not get some (maybe all) of those that PopUp from Controls of types msoControlPopup and msoControlButtonPopup.

As there may be a hierarchy of menus, of theoretically any depth, you need to rework your macro into a recursive process, not least because neither Name nor NameLocal is guaranteed unique for either a CommandBar or a Control.

From a quick look that will require quite a bit of rework. If you get stuck, do come back.

akokin
03-07-2008, 05:38 AM
Is this any use to you?

2007 Office System Document: Lists of Control IDs
http://www.microsoft.com/downloads/details.aspx?familyid=4329D9E9-4D11-46A5-898D-23E4F331E9AE&displaylang=en
Yes, it is. But it on English, and I need to Russian. Thus and so I need to the macro for building the list of controls IDs with russian name of commands (local language).
Thank you very much.

TonyJollans
03-07-2008, 09:51 AM
Yes, it is. But it on English, and I need to Russian. Thus and so I need to the macro for building the list of controls IDs with russian name of commands (local language).
Thank you very much.

Now I'm officially confused. Do you want the IDs of controls on Word 2003's toolbars or do you want the (English alpha) comand names for the buttons on Word 2007's Ribbon. Or something else? Where do you want to use the values you finally get?

akokin
03-07-2008, 11:17 AM
Tony, sorry for my bad English. I want to get the macro that will build list of the commands and them IDs for the buttons on Word 2007's Ribbon (and for other commands that miss on the Ribbon). Next I want to use these values (IDs) into the macro, that add into the shortcut Menu any commands or buttons, e.g. "Paste Special". That is sample:

Sub newItemToContextMenu()
Dim cb As CommandBar
Dim cbb As CommandBarButton
Dim cbc As CommandBarControl
CustomizationContext = NormalTemplate
On Error GoTo Ex
Set cb = CommandBars("text")
Set cbb = cb.FindControl(ID:=755)
If Not cbb Is Nothing Then Exit Sub
Set cbb = cb.Controls.Add(msoControlButton, 755, 1)
Set cbb = Nothing
Ex:
End SubIs there any way get that macro?

TonyJollans
03-07-2008, 01:56 PM
Your English is better than my Russian :)

What your code does is look at the ToolBars - which do still exist in Word 2007 - it does not look at the Ribbon. Yes, many of the same buttons exist on both but there may be some that don't and you cannot look at the Ribbon in VBA.

I have changed your code to look at all the toolbars. Is this helpful?



Option Explicit

Const strLabel As String = "Toolbar Name:"

Dim colID As Collection
Dim colCap As Collection

Sub GetAllControlIDOriginal()

Dim toolbarAll As CommandBar
Dim i As Long
Dim rgeDoc As Range

With Documents.Add
.PageSetup.Orientation = wdOrientLandscape
Set rgeDoc = .Range
rgeDoc.Collapse wdCollapseStart
End With

Set colID = New Collection
Set colCap = New Collection

For Each toolbarAll In Application.CommandBars
CheckToolBar toolbarAll
Next

For i = 1 To colID.Count
With rgeDoc
.InsertAfter colID(i) & vbTab
.InsertAfter colCap(i) & vbCrLf
If Trim(colID(i)) = strLabel Then
With .Paragraphs(.Paragraphs.Count).Range
With .Font
.Bold = True
.Size = 14
If Left(colID(i), 1) = " " Then .Size = 12
End With
With .ParagraphFormat
.SpaceBefore = 12
If Left(colID(i), 1) = " " Then .SpaceBefore = 3
.SpaceAfter = 3
.KeepWithNext = True
End With
End With
End If
End With
Next

rgeDoc.ConvertToTable vbTab
End Sub

Sub CheckToolBar(ToolBar As CommandBar, Optional Level As Long)

Dim ctrAll As CommandBarControl

colID.Add Space(4 * (Level)) & strLabel
colCap.Add Space(4 * (Level)) & ToolBar.Name
For Each ctrAll In ToolBar.Controls
If ctrAll.Type = msoControlPopup _
Or ctrAll.Type = msoControlButtonPopup Then
CheckToolBar ctrAll.CommandBar, Level + 1
Else
colID.Add Space(4 * (Level + 1)) & ctrAll.ID
colCap.Add Space(4 * (Level + 1)) & ctrAll.Caption
End If
Next

End Sub

akokin
03-07-2008, 08:57 PM
Tony, it's good! Yes, thank you very much! It's very helpful! It that I need.
Sincerely, Anton.