Consulting

Results 1 to 9 of 9

Thread: Solved: Onaction property

  1. #1

    Solved: Onaction property

    Hi,

    I am looking for help w.r.t. Onaction property.
    I got error while executing this program.
    Please advice where I went wrong.

    [VBA]

    Private Sub TestM()

    Dim cMenu1 As CommandBarControl

    Dim cbMainMenuBar As CommandBar

    Dim iHelpMenu As Integer

    Dim cbcCutomMenu As CommandBarControl

    Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")

    iHelpMenu = cbMainMenuBar.Controls("Help").Index

    Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu)

    cbcCutomMenu.Caption = "&Menu"

    With cbcCutomMenu.Controls.Add(Type:=msoControlButton)

    .Caption = "A&nt"

    .OnAction = "ANT"

    End With



    With cbcCutomMenu.Controls.Add(Type:=msoControlButton)

    .Caption = "&OT"

    .OnAction = "OT"

    End With


    With cbcCutomMenu.Controls.Add(Type:=msoControlButton)

    .Caption = "LA"

    .OnAction = "LA"

    End With

    Set cbcCutomMenu = cbcCutomMenu.Controls.Add(Type:=msoControlPopup)
    cbcCutomMenu.Caption = "Mo&re"

    With cbcCutomMenu.Controls.Add(Type:=msoControlButton) 'Added a contol to the sub menu

    .Caption = "&Charts"

    .FaceId = 420

    .OnAction = "ChartSheet"

    End With

    End Sub

    Private Sub ANT()
    msgbox "ANT"
    End Sub


    Private Sub OT()
    msgbox "OT"
    End Sub

    Private Sub LA()
    msgbox "LA"
    End Sub

    Private Sub ChartSheet()
    msgbox "ChartSheet"
    End Sub

    [/VBA]
    Attached Files Attached Files
    Last edited by justdriving; 09-10-2011 at 04:38 PM. Reason: Please refer attachment

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    Move the subs being called to a Standard Module.

    Hope that helps,

    Mark

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    And make them public!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @XLD:

    Most importantly, an overdue "Howdy!", how's everything?

    Quote Originally Posted by xld
    And make them public!
    I was going to include that, but by happenstance, I forgot to ditch the 'Private'(s) when moving the OP's subs to a standard module in testing. I near fell over when I caught that it was calling the Private Sub(s) fine!

    In recalling that OnAction acts like Application.Run when needing to provide the macroname and arguments all as a string, I did more testing to see if it would call a private procedure like Application.Run.

    Yep!

    Barring some discovery at work tomorrow (we now have 2010), .OnAction can call a private procedure in a standard module, in an object module (ThisWorkbook), or even pass an object variable to a private procedure in an object module!

    Mark

    Current personal motto: Ribbons are still for wrapping gifts...

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Quote Originally Posted by GTO
    Current personal motto: Ribbons are still for wrapping gifts...
    Welcome to Excel.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Aussiebear
    Welcome to Excel.
    Well, it is, as it should be, a family friendly environment here. Suffice it to say, 'Stellar,' 'Excel,' 'Great' and such are not the words escaping my mouth the past several weeks. I recall a post (albeit loosely) in which Bob gave some advice. I think the guy or gal was asking reference 2007 vs 2010 and I'm sure Bob said 2010. The point I absorbed though, was akin to "embrace it and don't look back.' Certainly sensible and of course I am simply now experiencing what most did several years ago. On the bright side, I got to skip 2007 at least. Still, you might as well expect to hear some whining (probably not that much). I find looking for stuff that I knew where it was before aggravating. That, and I do not need 15 bajillion "pictures" (icons). That's just slower...

    I would in fairness give 'props' for some stuff. Whilst I don't really need a preview of what certain paste special options are going to look like (and find the screen flicker irritating), I think its a neat idea. Not sure if it'll keep those who need the help from picking the wrong choice, but minus the preview, I like the option in the context menu.

  7. #7
    Hi all,

    Questions and answers helped me learn more. I have one more query related to above VBA program. This VBA program will create Menu on Excel as following: -


    Menu
    ||
    ANT
    ||
    OT
    ||
    LA
    ||
    MORE = CHARTS

    I want to change this structure


    Menu
    ||
    ANT
    ||
    OT
    ||
    --
    LA = LosAngeles
    LA = California
    --
    ||
    MORE = CHARTS

    Here, "=" represents Sub-branches and
    two "LA" represents a single "LA" having two sub-branches "LosAngeles" and "California".

    Please advice how can I do it?

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub TestM()
    Dim cMenu1 As CommandBarControl
    Dim cbMainMenuBar As CommandBar
    Dim iHelpMenu As Integer
    Dim cbcCutomMenu As CommandBarControl

    Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
    iHelpMenu = cbMainMenuBar.Controls("Help").Index

    Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu)
    cbcCutomMenu.Caption = "&Menu"

    With cbcCutomMenu

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "A&nt"
    .OnAction = "ANT"
    End With

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "&OT"
    .OnAction = "OT"
    End With

    With .Controls.Add(Type:=msoControlPopup)

    .Caption = "LA"

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "Los Angeles"
    .OnAction = "LA"
    End With

    With .Controls.Add(Type:=msoControlButton)

    .Caption = "California"
    .OnAction = "Calif"
    End With
    End With
    End With

    Set cbcCutomMenu = cbcCutomMenu.Controls.Add(Type:=msoControlPopup)
    cbcCutomMenu.Caption = "Mo&re"

    With cbcCutomMenu.Controls.Add(Type:=msoControlButton) 'Added a contol to the sub menu

    .Caption = "&Charts"
    .FaceId = 420
    .OnAction = "ChartSheet"
    End With
    End Sub[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Thanks to all of you. It solved my problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •