Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi. I really hope that someone can help me with this.

    I can create menu items and buttons on shortcut menus and can delete them from the main menu but I can't delete them from the procedure called via the OnAction method. (I want to manipulate the menu structure and do other tasks in the called procedure via the menu button's OnAction method.)

    Here is the code:

    '---------------------------------------------------------------
    'The MainMenYou procedure creates the MainMenu menu item on the
    'Headings shortcut menu. Sub menus are added to this menu in the
    'procedure called by the .OnAction method.
    '---------------------------------------------------------------

    Public Sub MainMenYou()

    Dim cbpop As CommandBarControl

    CustomizationContext = ActiveDocument.AttachedTemplate

    Set cbpop = CommandBars("Headings").Controls.Add( _
    Type:=msoControlPopup, Before:=1)
    cbpop.Caption = "=> Main Menu"
    cbpop.Tag = "MainMenu"
    cbpop.OnAction = "SubMenYou"

    End Sub

    '-------------------------------------------------------------
    'The SubMenYou procedure below adds or modifies menus to the
    'MainMenu menu item created above.
    '-------------------------------------------------------------

    Sub SubMenYou()

    CustomizationContext = ActiveDocument.AttachedTemplate

    Dim cbpop As CommandBar
    Dim oCustomMenuItem As CommandBarControl
    Dim sMenuItemTag As String
    Dim cbsub2 As CommandBarControl
    Dim cbsub3 As CommandBarControl
    Dim cbctl2 As CommandBarControl


    sMenuItemTag = "MainMenu"
    Set cbpop = Application.CommandBars("Headings")
    Set oCustomMenuItem = cbpop.FindControl(Tag:=sMenuItemTag)

    Set cbsub2 = oCustomMenuItem.Controls.Add(Type:=msoControlPopup )
    cbsub2.Visible = True
    cbsub2.Caption = "Submenu 1"
    cbsub2.Tag = "Submenu1"

    Set cbsub3 = cbsub2.Controls.Add(Type:=msoControlPopup)
    cbsub3.Visible = True
    cbsub3.Caption = "Submenu 2"
    cbsub3.Tag = "Submenu2"

    '--------------------------------------------------------------------------
    'Here is the problem. Either FindControl can't find the menu control or
    'deletion from the sub-procedure called by .OnAction isn't allowed.
    'The result is that it adds an extra menu item each time without deleting
    'any.
    '--------------------------------------------------------------------------

    sMenuItemTag = "SubmenuButton"
    Set oFileMenu = Application.CommandBars("Headings")
    Set oCustomMenuItem = oFileMenu.FindControl(Tag:=sMenuItemTag)

    If Not oCustomMenuItem Is Nothing Then
    oCustomMenuItem.Delete
    Else
    Set cbctl2 = cbsub3.Controls.Add(Type:=msoControlButton)
    cbctl2.Visible = True
    cbctl2.Style = msoButtonCaption
    cbctl2.Caption = "Submenu Button"
    cbctl2.Tag = "SubmenuButton"
    End If

    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The problem is that SubMenYou unconditionally creates new submenus "SubMenu 1" and "SubMenu 2" each time it is run. So FindControl searches in the instance of "SubMenu 2" that has just been created and hence doesn't find the "SubMenu Button" item - that exists only in previously created instances of "SubMenu 2".

    Why would you want to add items each time the user clicks the "=> Main Menu" item? Isn't is sufficient to create them once, in MainMenYou?

  3. #3
    New Lounger
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='777629' date='30-May-2009 17:28']The problem is that SubMenYou unconditionally creates new submenus "SubMenu 1" and "SubMenu 2" each time it is run. So FindControl searches in the instance of "SubMenu 2" that has just been created and hence doesn't find the "SubMenu Button" item - that exists only in previously created instances of "SubMenu 2".

    Thanks. Appreciate your point but:
    a. if the SubMenu button item does exist (which it does after the first time I run it, it still doesnt find it
    b. the problem exists even if I write the procedure more simply with only one control under the MainMenu menu item


    Why would you want to add items each time the user clicks the "=> Main Menu" item? Isn't is sufficient to create them once, in MainMenYou?

    I want to dynamically change the context-sensitive menu items available to the user.[/quote]

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The line

    Set oCustomMenuItem = oFileMenu.FindControl(Tag:=sMenuItemTag)

    searches in oFileMenu only. If you want it to search in submenus too, use

    Set oCustomMenuItem = oFileMenu.FindControl(Tag:=sMenuItemTag, Recursive:=True)

    But you'll still have the problem that you create SubMenu 1 and SubMenu 2 unconditionally. If the user selects => Main Menu 37 times, he/she will end up with 37 copies of SubMenu 1 and SubMenu 2.

  5. #5
    New Lounger
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='777635' date='30-May-2009 19:03']The line

    Set oCustomMenuItem = oFileMenu.FindControl(Tag:=sMenuItemTag)

    searches in oFileMenu only. If you want it to search in submenus too, use

    Set oCustomMenuItem = oFileMenu.FindControl(Tag:=sMenuItemTag, Recursive:=True)

    But you'll still have the problem that you create SubMenu 1 and SubMenu 2 unconditionally. If the user selects => Main Menu 37 times, he/she will end up with 37 copies of SubMenu 1 and SubMenu 2.[/quote]


    Hans, you are a MasterBlaster! The Recursive property did the trick.

    Many thanks. I had spent many days trying to figure that out.

Posting Permissions

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