Solved

Referencing Custom Item On Command Bar

Posted on 2011-03-22
8
518 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

914 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