Solved

Referencing Custom Item On Command Bar

Posted on 2011-03-22
8
521 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

752 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