Solved

Referencing Custom Item On Command Bar

Posted on 2011-03-22
8
519 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

803 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