Capture mouses X &Y position on a worksheet

S

Stewart

Capture mouses X&Y position on a worksheet. I want to use
the before right click option of the worksheet to display
a custom toolbar. I want to use the current position of
the mouse to set the left and top parameters for the
toolbar.

Is there a better way?
 
J

Jake Marx

Hi Stewart,

Are you trying to show a popup menu that you've created? If so, you don't
need to know the coordinates; you can use the ShowPopup method without any
arguments to show the menu.

Here's some (very ugly) code that gives you a quick overview of how you
could accomplish this. Typically, you'd want to create your menu upon
workbook open, pop it up as needed, and destroy it at workbook close.

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, _
Cancel As Boolean)
On Error Resume Next
Application.CommandBars("jake").Delete
On Error GoTo 0

With Application.CommandBars.Add("jake", msoBarPopup, , True)
With .Controls.Add(msoControlPopup)
.Caption = "&Edit"
With .Controls.Add(msoControlButton)
.Caption = "&Copy"
End With
With .Controls.Add(msoControlButton)
.Caption = "&Paste"
End With
End With
.ShowPopup
End With

Cancel = True
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
G

Guest

Got it - thanks!
-----Original Message-----
Hi Stewart,

Are you trying to show a popup menu that you've created? If so, you don't
need to know the coordinates; you can use the ShowPopup method without any
arguments to show the menu.

Here's some (very ugly) code that gives you a quick overview of how you
could accomplish this. Typically, you'd want to create your menu upon
workbook open, pop it up as needed, and destroy it at workbook close.

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, _
Cancel As Boolean)
On Error Resume Next
Application.CommandBars("jake").Delete
On Error GoTo 0

With Application.CommandBars.Add("jake", msoBarPopup, , True)
With .Controls.Add(msoControlPopup)
.Caption = "&Edit"
With .Controls.Add(msoControlButton)
.Caption = "&Copy"
End With
With .Controls.Add(msoControlButton)
.Caption = "&Paste"
End With
End With
.ShowPopup
End With

Cancel = True
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

Capture mouses X&Y position on a worksheet. I want to use
the before right click option of the worksheet to display
a custom toolbar. I want to use the current position of
the mouse to set the left and top parameters for the
toolbar.

Is there a better way?

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top