+ Reply to Thread
Results 1 to 14 of 14
  1. #1
    Forum Contributor
    Join Date
    08-09-2005
    MS-Off Ver
    2003 & 2007
    Posts
    111

    add button to right-click menu in External Data range

    I'd like to add a button to the top of the right-click (context) menu. I know how to do this with CommandBars("Cells"), CommandBars("Row") and CommandBars("Column").

    But how do I do this when the range contains data from an external source?

    I can add a button to CommandBars("External Data") in the same way as with the other CommandBars, but it doesn't actually appear. And the buttons in this CommandBar are only the ones in the bottom section of the pop-up menu. The top half looks like the regular "Cells" menu.

    How does this stuff work?

    ...

    A separate question. All the right-click menu examples I've seen use the BeforeRightClick event. Why not just use the Activate event? Is there a functional advantage with BeforeRightClick? In the Activate event, it wouldn't have to run as often.
    Last edited by dlh; 07-21-2010 at 08:19 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,497

    Re: add button to right-click menu in External Data range

    Hello dlh,

    It isn't clear to me what you are trying to do. If you know how to add a button to a menu then what problems are you experiencing?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    08-09-2005
    MS-Off Ver
    2003 & 2007
    Posts
    111

    Re: add button to right-click menu in External Data range

    When I add a button to CommandBars("External Data") it does not appear on the right-click menu in an external data range as I had expected.

    Code:
    Set CustomMenuItem = Application.CommandBars("External Data").Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)
    With CustomMenuItem
        .Caption = "&My Macro"
        .OnAction = "RunMyMacro"
        .Tag = "dlh-custom"
    End With

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,497

    Re: add button to right-click menu in External Data range

    Hello dlh,

    Which version of Excel are you coding this in 2003 or 2007? In 2003 the "External Data" menu is not a context menu.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    08-09-2005
    MS-Off Ver
    2003 & 2007
    Posts
    111

    Re: add button to right-click menu in External Data range

    I'm developing in 2003. But I need this to work in 2007 also.

    For what it's worth, I've confirmed that my button was successfully added to the CommandBar. But it doesn't appear on screen.
    Last edited by dlh; 07-21-2010 at 06:46 PM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,497

    Re: add button to right-click menu in External Data range

    Hello dlh,

    It would help to see the code you have written. Can you post it?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Contributor
    Join Date
    08-09-2005
    MS-Off Ver
    2003 & 2007
    Posts
    111

    Re: add button to right-click menu in External Data range

    Here's my code:

    Code:
    Public Sub LoadCustomMenu()
    Dim CustomMenuItem As CommandBarButton
    
    Call UnloadCustomMenu
    
    'create Right-click menu item
    
    Set CustomMenuItem = Application.CommandBars("External Data").Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)
    With CustomMenuItem
        .Caption = "&My Macro"
        .OnAction = "RunMyMacro"
        .Tag = "dlh-custom"
    End With
    
    Set CustomMenuItem = Application.CommandBars("Row").Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)
    With CustomMenuItem
        .Caption = "&My Macro"
        .OnAction = "RunMyMacro"
        .Tag = "dlh-custom"
    End With
    
    Set CustomMenuItem = Application.CommandBars("Cells").Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)
    With CustomMenuItem
        .Caption = "&My Macro"
        .OnAction = "RunMyMacro"
        .Tag = "dlh-custom"
    End With
    
    End Sub
    
    Public Sub UnloadCustomMenu()
    
    If Not Application.CommandBars("External Data").FindControl(Tag:="dlh-custom") Is Nothing Then
        Application.CommandBars("External Data").FindControl(Tag:="dlh-custom").Delete
    End If
    
    If Not Application.CommandBars("Row").FindControl(Tag:="dlh-custom") Is Nothing Then
        Application.CommandBars("Row").FindControl(Tag:="dlh-custom").Delete
    End If
    
    If Not Application.CommandBars("Cells").FindControl(Tag:="dlh-custom") Is Nothing Then
        Application.CommandBars("Cells").FindControl(Tag:="dlh-custom").Delete
    End If
    
    End Sub

    These routines are called in ThisWorkbook's code module.

    Code:
    Private Sub Workbook_Activate()
        Call LoadCustomMenu
    End Sub
    
    Private Sub Workbook_Deactivate()
        Call UnloadCustomMenu
    End Sub

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,497

    Re: add button to right-click menu in External Data range

    Hello dlh,

    The button is there, you just need to give it a FacecID. The menu entries like File, Edit, etc. are Popup menus. That's why they appear as labels. Button controls require an icon (FacecID) to be visible.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,497

    Re: add button to right-click menu in External Data range

    Hello dlh,

    This code adds a generic FaceID for running a macro...
    Code:
    Sub TestA()
    
     Dim CustomMenuItem As Object
     
        Set CustomMenuItem = Application.CommandBars("External Data").Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)
        
          With CustomMenuItem
            .Caption = "&My Macro"
            .FaceId = 2151
            .OnAction = ""
            .Tag = "dlh-custom"
          End With
    
       Application.CommandBars("External Data").Visible = True
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Forum Contributor
    Join Date
    08-09-2005
    MS-Off Ver
    2003 & 2007
    Posts
    111

    Re: add button to right-click menu in External Data range

    Sorry, but I don't think FaceId is related to my problem. That just specifies an icon to appear on the left side of the button. If you don't specify a value for FaceId, it defaults to 1 which is a blank icon.

    To be specific about my problem: my custom button appears correctly on the "Cells" and "Row" CommandBars, but not on the "External Data" CommandBar.

  11. #11
    Forum Contributor
    Join Date
    08-09-2005
    MS-Off Ver
    2003 & 2007
    Posts
    111

    Re: add button to right-click menu in External Data range

    Aha. Based on your example code, I realize that "External Data" is not the CommandBar that I thought it was.

    So when you right-click on a cell within an external data range, what is the name of the CommandBar that appears? It's equivalent to "Cells" for a non-external-data range.

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,497

    Re: add button to right-click menu in External Data range

    Hello dlh,

    I only see one CommandBar named "External Data". It could be that another menu attaches the "External Data" menu to itself. Here is a macro I wrote to list all the CommandBars along with other information about them. It may help you track down what you are looking for.
    Code:
    'Written: May 31, 2008
    'Updated: May 18, 2010
    'Author:  Leith Ross
    'Summary: Lists all the avaiable menus in Excel on a workhseet. It provides the
    '         menu name, whether it is visible, the type, and if it is enabled.
    
    Sub ListExcelMenus()
    
      Dim arr As Variant
      Dim C As Variant
      Dim CmdBar As Object
      Dim R As Long
      Dim T As String
      
        C = "A"
        R = 1
        
          With Range("A1:E1")
            .Value = Array("Name", "Visible", "Type", "Enabled", "ID")
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
          End With
          
            For I = 1 To Excel.CommandBars.Count
              Set CmdBar = Excel.CommandBars(I)
              With CmdBar
                Select Case .Type
                  Case 0
                    T = "Normal"
                  Case 1
                    T = "Menu"
                  Case 2
                    T = "Popup"
                End Select
                arr = Array(.Name, .Visible, T, .Enabled, .ID)
                Cells(R + I, C).Resize(1, 5).Value = arr
              End With
            Next I
          
           Range("A:E").Columns.AutoFit
           
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  13. #13
    Forum Contributor
    Join Date
    08-09-2005
    MS-Off Ver
    2003 & 2007
    Posts
    111

    Re: add button to right-click menu in External Data range

    The correct CommandBar is called "Query". (Thank you, Leith. Your list helped.)

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,497

    Re: add button to right-click menu in External Data range

    Hello dlh,

    Glad you found it. Thanks for sharing the solution.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0