Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    capturing right click (VBA/Excel/2000/SP3)

    Does anyone know how to capture the right click (or context key press) which brings up the context menu in the VBE project explorer in order to run some code? I want to alter the menu items according to what is selected.

    I tried using the same CommandBarEvents method as for menu items themselves but it complains that correct automation is not supported. I'm sure this is because it's a popup and not a button, so another method is needed.

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: capturing right click (VBA/Excel/2000/SP3)

    To attach a macro to a menuitem in the VBE, you need a class module.

    This book describes how to do that:

    Excel 2000/2002 VBA Programmers Reference. Stephen Bullen, John Green, Rob Bovey, Robert Rosenberg. Wrox Press.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: capturing right click (VBA/Excel/2000/SP3)

    I've already successfully attached macros to buttons on menus using the CommandBarEvents object inside my own wrapper class, just as you suggest. The problem is that now I want to attach it to the right click itself, which shows the context menu. The CommandBarEvents object doesn't allow me to do this.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: capturing right click (VBA/Excel/2000/SP3)

    So you are saying you cannot set a rightclick event inside the vbe, right?

    I guess you should be able to add/remove items in that menu, so I have no idea why one wouldn't be able to attach a macro too.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: capturing right click (VBA/Excel/2000/SP3)

    Yes, adding/removing items to and from the menu and attaching macro's to those items is no problem.

    The problem is that I want to add/remove items according to which item is selected in the project explorer.

    The are, in fact, probably 3 ways to do this:-

    1) capture any right click (or context menu key press) event in the VBE, determine if the project window context menu is being shown, interrogate the selected item and add an item to the context menu accordingly
    2) capture the specific right click (or context menu key press) event in the project window (or the event of showing that particular context menu), interrogate the selected item and add an item to the context menu accordingly
    3) capture the selection change in the project window and add the item to the context menu at that time (regardless of when the menu is actually shown)

    At present I don't know how to capture any of these events and that is my problem.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: capturing right click (VBA/Excel/2000/SP3)

    AFAIK Stephen Bullen has managed to do something with capturing hotkeys in the VBE, not sure if he ever found out how to catch (right) clicks. look at his site: http://www.bmsltd.ie and see if there is anything useful there. Maybe you could even send him an email message (you may refer to me).
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: capturing right click (VBA/Excel/2000/SP3)

    Not sure if this will help you as I don't fully understand your question, but attached is a right click menu which I use for a series of image controls.
    If it's of any help then, all good, if it's not, then it may be of use to another forum member:

    <pre>Private Function imgRclick(i As Integer, Button As Integer)

    Dim oMenu As cPOPupMenu
    Dim lMenuChosen As Long
    Dim intIndex As Integer
    Dim strFile As String
    Dim j As Integer

    If Button = vbRightButton Then
    If intNumberOfImages = 0 Then Exit Function
    Set oMenu = New cPOPupMenu
    lMenuChosen = oMenu.Popup("Add Image To Mail List", "-", "Zoom Image", _
    "-", "Add or View Comment", "-", "Print This Image")
    For j = 1 To intNumberOfImages
    If j = i Then

    Select Case lMenuChosen
    Case 1
    Me.lstEmail.AddItem strFolder & "" & _
    Left$(Me.Controls("Text" & i), 8) & ".jpg"
    Me.txtInfo.Text = Me.lstEmail.ListCount & _
    " Images To Mail"

    Case 3
    frmZoom.Show
    frmZoom.txtPicFile = strFolder & "" & _
    Left$(Me.Controls("Text" & i), 8) & ".jpg"
    frmZoom.SuperPicCtl1.LoadImage (frmZoom.txtPicFile.Text)

    Case 5
    Dim stAppName As String
    If Me.Controls("Text" & i).Text = vbNullString Then
    MsgBox "No Image To Comment", _
    vbInformation, "Information"
    End If
    stAppName = "C:MM-Utilitiesedjpgcom.exe" & _
    " " & "L:MMPDFImage" & _
    Left$(Me.Controls("Text" & i), 8) & ".jpg"
    Call Shell(stAppName, 1)

    Case 7
    frmPrintMain.Show
    frmPrintMain.txtFileName.Text = strFolder & "" & _
    Left$(Me.Controls("Text" & i), 8) & ".jpg"

    Case 0
    ' Do Nothing
    End Select
    Else
    End If
    Next j
    End If
    End Function
    </pre>


Posting Permissions

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