Enumerating and querying CommandBarButtons on cust command bars?

J

JB

Hi Folks,
Just looking for a way to get information out of custom command bars and
the buttons related to them.

I have a few custom command bars and I'd like to know how I can get all
the info from them (possibly to re-create them on the fly)

Any pointers appreciated (excuse the test array I'll write some proper
code when I get it working :) )

I started with something like this.....

Dim cb As Office.CommandBar
Dim cbutt As Office.CommandBarButton

Dim strcbname As String
Dim test(25) As String

For Each cb In CommandBars
If Not cb.BuiltIn Then
test(1) = cb.AdaptiveMenu
test(2) = cb.Application
test(3) = cb.Context
'test(4) = cb.Controls 'not working
test(5) = cb.Creator
test(6) = cb.Height
test(7) = cb.Index
test(8) = cb.Left
test(9) = cb.Name
test(10) = cb.NameLocal
test(11) = cb.Parent
test(12) = cb.Position
test(13) = cb.RowIndex
test(14) = cb.Type
test(15) = cb.Width

For Each cbutt In cb ' This is where it falls over. Don't mind
writing over array items here! Just testing in debug for now
test(1) = cbutt.Application
test(2) = cbutt.BeginGroup
test(3) = cbutt.BuiltIn
test(4) = cbutt.BuiltInFace
test(5) = cbutt.Caption
test(6) = cbutt.DescriptionText
test(7) = cbutt.FaceId
test(8) = cbutt.Height
test(9) = cbutt.ID
test(10) = cbutt.Index
test(11) = cbutt.IsPriorityDropped
test(12) = cbutt.Left
test(13) = cbutt.Mask
test(14) = cbutt.OnAction
test(15) = cbutt.Parameter
test(16) = cbutt.Picture
test(17) = cbutt.Priority
test(18) = cbutt.ShortcutText
test(19) = cbutt.State
test(20) = cbutt.Style
test(21) = cbutt.Tag
test(22) = cbutt.TooltipText
test(23) = cbutt.Top
test(24) = cbutt.Type
test(25) = cbutt.Width
Next
Next
 
C

Chad DeMeyer

JB,

You didn't specify a collection to iterate through in the second "For ...
Each" construct:

"For Each cbutt In cb"

Also, although not intuitively obvious, the collection to work with is
CommandBarControls, not CommandBarButtons, since not all controls on the
CommandBar are necessarily of the type CommandBarButton. So you need to
rewrite the following two lines as shown:

Dim cbutt As Office.CommandBarControl
For Each cbutt In cb.Controls

Regards,
Chad
 
J

JB

Chad said:
JB,

You didn't specify a collection to iterate through in the second "For ...
Each" construct:

"For Each cbutt In cb"

Also, although not intuitively obvious, the collection to work with is
CommandBarControls, not CommandBarButtons, since not all controls on the
CommandBar are necessarily of the type CommandBarButton. So you need to
rewrite the following two lines as shown:

Dim cbutt As Office.CommandBarControl
For Each cbutt In cb.Controls

Regards,
Chad
Chad You Da Man!!
Thanks for that.

Only problem I got now is how to index all this info in arrays :)

I got 6 toolbars with around 7 controls on each and I wanna stick the
related info in an .ini file to edit/re-create toolbars later.

I was gonna use dynamic arrays (which I'm usually quite good at) but
this is givin me some serious block! :)

Any Ideas

Cheers

J
 
C

Chad DeMeyer

JB,

I've never had occasion to do it before, so I'm afraid I don't have any
'get-you-going' types of ideas at this time. The only suggestion I'd make
at this point is that you define your array as variant, since the different
property values you are populating it with may be (and probably are) of more
than one data type. Or, this might be one of those occasions when it would
be helpful to create a user-defined data type.
That's my 2 cents. Any gurus got a nickel out there?

Regards,
Chad
 

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