How To Reference Item in toolbar by Code

G

Gary

I have created a toolbar with the following code:

Set cbars = Application.CommandBars
Set cbar = cbars.Add(Name:="XYJ", Position:=msoBarTop, Temporary:=True)

Set cbButton1 = cbar.Controls.Add(Type:=msoControlButton)
With cbButton1
.Caption = " Skill "
.TooltipText = "Click for Skill"
.Tag = "cbbVBAMacro"
.Style = msoButtonCaption
.OnAction = "ThisDocument.SkillLayerActive"
End With


I wish to change the caption from skill to SKILL in the onAction routine.
I don't know how to reference the caption in the cbButton1. Can someone
help!!

Thanks,
 
A

Al Edlund

you might try something like this

al

Public Sub setCbar()

Set cBars = Application.CommandBars
For Each cBar In cBars
If cBar.Name = "Skill" Then
MsgBox "Control Bar Already Defined"
Exit Sub
End If
Next cBar

Set cBar = cBars.Add(Name:="Skill", _
Position:=msoBarTop, Temporary:=True)
cBar.Visible = True

Set cCtrl = cBar.Controls.Add(Type:=msoControlButton, ID:=2)
With cCtrl
.Caption = "Skill"
.TooltipText = "Click for Skill"
.Tag = "cbbVBAMacro"
.Style = msoButtonCaption
.OnAction = "ThisDocument.SkillLayerActive"
End With


End Sub

Public Sub SkillLayerActive()

Set cBars = Application.CommandBars
Set cBar = cBars("Skill")
If 0 < cBar.Controls.Count Then
For intX = 1 To cBar.Controls.Count
Set cCtrl = cBar.Controls(intX)
' check for mixed case first
If cCtrl.Caption = "Skill" Then
cCtrl.Caption = "SKILL"
End If
Next intX
End If

End Sub
 
G

Gary

It works!! Thank You

Would you know if its possible to change the color of the word SKILL to
another color???

Thanks
 
G

Gary

I have a little problem with I change the .caption to something else. When I
exit, the system ask if I wish to save changes and I say no, and I then exit.
MS Message is generated if I wish to send to message to MS.

I noticed that after the no to save, then quit from the application, then
exit - No MS message is generated.

When I am opening this as an embed object in power point, I get the same
message. It makes it not very clean to use by others.

I found out that the statement:

If cCtrl.Caption = "Skill" Then
cCtrl.Caption = "SKILL"
End If

Cause the issue. The statement cCtrl.Caption = "SKILL" cause an change and
I say no to the save and exit (that cause the MS message to Microsoft if I
want to send).

Can someone help with this.

Thanks,

G
 
A

Al Edlund

be careful. The help facilities for vba list a lot of different information
where it does not apply to all products the same way.
powerpoint<>visio<>word<>excel.
al
 
G

Gary

Was there a way to change the color on the caption in your solution stated
above???

I also found a MS article that states this issue on the warning. Work
around was to quit then exit. It does work, but users would find it ackward

Thanks,

Gary
 
A

Al Edlund

suggest you check the vba help for commandbarcontrols properties. short
answer no color is not an option
al
 

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