Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Referencing Custom Item On Command Bar

Posted on 2011-03-22
8
Medium Priority
?
523 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

704 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