Solved

Referencing Custom Item On Command Bar

Posted on 2011-03-22
8
517 Views
Last Modified: 2012-05-11
On the standard menu bar I have a custom entry, "Menu Test." I know to reference the catgory Menu Test on the bar by running the following to get the ID e.g.,
Set myId = CommandBars("Worksheet Menu Bar").Controls("MenuTest")
   MsgBox myId.Caption & Chr(13) & myId.ID

I need a way to reference the msoControlButton "Pre-Selected" and can't figure this out. Need some help please.
 screen shot of custom menu entry
Sub CreateSSMenu_Wcodestyle2()

Dim cbpop As CommandBarControl, cbctl As CommandBarControl, cbsub As CommandBarControl
Call RemoveSSMenu_Wcodestyle

Set cbpop = CommandBars("Worksheet Menu Bar").Controls.Add(Type:=msoControlPopup, Temporary:=True)
With cbpop
 .Caption = "MenuTest"
 .Visible = True
End With

Set cbsub = cbpop.Controls.Add(Type:=msoControlPopup)
With cbsub
  .BeginGroup = True 'False '
  .Caption = "Address To"
End With

Set cbctl = cbsub.Controls.Add(Type:=msoControlButton)
With cbctl
  .Visible = True
  .BeginGroup = True 'False 'True
  .Style = msoButtonCaption
  .Enabled = False
 .Caption = "Pre-Selected" 
End With

Set cbctl = cbsub.Controls.Add(Type:=msoControlButton)
With cbctl
  .Visible = True
  .BeginGroup = True 'False
  .Style = msoButtonCaption
  .Caption = "User Specified"
End With

Set cbpop = Nothing
Set cbsub = Nothing
Set cbctl = Nothing

End Sub

Open in new window

0
Comment
Question by:ronaldj
  • 4
  • 4
8 Comments
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 35190792
Hi ronaldj,

You do not have the .Tag defined nor do you have an .OnAction defined.


.OnAction = "MakeEmail" 'the macro to run
.Tag = "JobRelease" 'refers to the button like name

Open in new window

0
 

Author Comment

by:ronaldj
ID: 35191649
dragontooth:

Sorry I wasn't clearer. Intentionally, there is no action associated with "Pre-Selected". This is the generic default case. It is presented for informational purposes only. When the user opens the workbook, by actual default "Pre Selected "is presented as "Pre-Selected - Mr A"...a project leader lets say, always included...user goes to "User Specified"... UF opens and selects Mr C and Mr. D, click Set, UF closes...Menu updates and now reads Pre-Selected - Mr A, Mr. B, Mr. C" Like I said, don't know how to reference msoControlButton "Pre-Selected"...never used tag before and cannot find examples. Can you provide? Thanks, Ron
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 35192210
Assign a .Tag to the buttons, then seta var to the commandbar ("cbpop") in above code.
Read in the helpfiles about the findcontrol method. You can use id and type but to get the only one you must use the tag property and when you assign a tag it MUST be unique.

Then you can
Set cbBut = cbpop.FindControl(, , "ScheduleForm")
With cbBut
  .Caption = "Pre-Selected - Mr A, Mr. B, Mr. C"
End With

HTH

dragontooth


Sub Auto_Close()
     'delete the button
    On Error Resume Next
    Dim cbpop As CommandBar
    Dim cbBut As CommandBarButton
    SaveIt
    Set cbpop = Application.CommandBars("Worksheet Menu Bar")
    Set cbBut = cbpop.FindControl(, , "ScheduleForm")'"ScheduleForm" is the tag of a button on this commandbar
    cbBut.Delete
    Set cbBut = cbpop.FindControl(, , "VacationForm")'"VacationForm" is the tag of a button on this commandbar
    cbBut.Delete
    cb.Delete
    Err.Clear
    On Error GoTo 0
End Sub

Open in new window

0
 

Author Comment

by:ronaldj
ID: 35193217
tried the code below in a blank workbook

recieved error Objject Required at line
Set cbBut = cbpop.FindControl(, , "TheCurrentDrive")

What did I screw up?



Set cbctl = cbsub.Controls.Add(Type:=msoControlButton)
With cbctl
  .Visible = True
  .BeginGroup = True 'False 'True
  .Style = msoButtonCaption
  .Enabled = False
 .Caption = "Pre-Selected" 
 .Tag = "TheCurrentManager"
End With


If ActiveWorkbook.Path = "" Then
CurrentDrive_OnLoad = " - "
Set cbBut = cbpop.FindControl(, , "TheCurrentManager")
With cbBut
  .Caption = "Pre-Selected Drive(s) [ " & CurrentDrive_OnLoad & " ]"
End With
Exit Sub
End If

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ronaldj
ID: 35193538
dragontooth:

Got it! Other assorted code not included but the 2 lines below achieve the desired result. The first line is placed before a series of nested  If...Else statements to determine the type of Case...the second line is resident in each of the unique cases. Could probably clean up a bit but right now ain't touching a thing! Please respond so I can reward points...your the only responder and were pretty darn close, couldn't have got there without your help...Thanks again, Ron
Set cbBut = CommandBars(1).FindControl(msoControlButton, , _
    "TheCurrentDrive", , True)

With cbBut
  .Caption = "Pre-Selected Drive(s) [ " & CurrentDrive_OnLoad & " ]"
End With

Open in new window

0
 
LVL 14

Accepted Solution

by:
Tommy Kinard earned 500 total points
ID: 35193743
That is going to be dangerous Ron, CommandBars(1) if the control is not there it will throw up. See the code I am posting it may take a little playing to make it work with what you are working with but I tried to make it as generic as possible.


Sub GetCommandBar(cbpop As CommandBarControl)
    On Error Resume Next
    Set cbpop = CommandBars("Worksheet Menu Bar")
    If Err.Number <> 0 Then
        MsgBox "CommandBar not found!"
        Err.Clear
    End If
    On Error GoTo 0
End Sub
Sub GetCommandBarButton(cbpop As CommandBarControl, cbsub As CommandBarControl)
    Set cbsub = cbpop.FindControl(, , "TheCurrentManager")
End Sub

Open in new window

0
 

Author Closing Comment

by:ronaldj
ID: 35193842
good job!
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 35193894
Thanks for the points and the grade!

dragontooth
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
TT Copy Formula 3 16
File size limit in SharePoint 2010 3 15
Excel Worksheet Function of VBA Code 5 10
TT Formula Check 9 4
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now