View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
None None is offline
external usenet poster
 
Posts: 15
Default Custom Mouse over Cell Event Help

I will chack it out. Thanks!

On Mon, 11 Dec 2006 16:10:36 +0800, "NickHK"
wrote:

Using Karl's timer class
http://vb.mvps.org/samples/project.asp?id=TimerObj

This seems to work. You need to test more and save often, because if you End
abruptly you will crash Excel.
Make sure you toggle the timer, not just stop your code. Reading Karl's
closely, you can probably fix this.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, _
ByVal
lpWindowName As String) _
As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA"
(ByVal hWnd1 As Long, _
ByVal hWnd2
As Long, _
ByVal lpsz1
As String, _
ByVal lpsz2
As String) _
As Long

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) _
As Long

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long,
_
ByVal yPoint
As Long) _
As Long


Private Type POINTAPI
X As Long
Y As Long
End Type


Private WS_Hwnd As Long
Private CursorPos As POINTAPI
Private CursorCell As Range

Dim WithEvents Timer1 As cTimer

Private Sub CommandButton1_Click()
Dim RetVal As Long

If Timer1 Is Nothing Then
Set Timer1 = New cTimer
Timer1.Interval = 250

'XL2002+ has Application.hWnd, but FindWindow etc will work on all,
assuming the class names have remained the same
'Check other versions; this works for 2002

'Get the HWnd of WS in question
RetVal = FindWindowEx( _
FindWindowEx( _
FindWindow("XLMAIN", Application.Caption) _
, 0, "XLDESK", vbNullString) _
, 0, "EXCEL7", ActiveWindow.Caption)

If RetVal = 0 Then
MsgBox "Cannot get Window handle."
Exit Sub
End If

WS_Hwnd = RetVal

End If

'Toggle the timer
With Timer1
.Enabled = Not .Enabled
End With

End Sub

Private Sub Timer1_Timer()
Dim RetVal As Long

RetVal = GetCursorPos(CursorPos)

If RetVal = 0 Then
MsgBox "Cannot get cursor position."
Exit Sub
End If

RetVal = WindowFromPoint(CursorPos.X, CursorPos.Y)

'See if we are in the required window
If RetVal = WS_Hwnd Then
'See if we are on the required WS
If ActiveSheet.Name < Me.Name Then
Exit Sub
End If
Else
Exit Sub
End If

On Error Resume Next
Set CursorCell = Application.Windows(1).RangeFromPoint(CursorPos.X,
CursorPos.Y)

Debug.Print CursorPos.X, CursorPos.Y;

If Err.Number = 0 Then
Debug.Print CursorCell.Address
Else
Debug.Print "Err"
End If

End Sub

NickHK

"NickHK" wrote in message
...
Yes Excel knows where the mouse is, but it is not exposed natively.
You could may use the GetCursorPos API to get the cursor position

(checking
if you actually in Excel at that time with WindowFromPoint), translate to
Excel coordinates and use RangeFromPoint to see if you are over the

required
range. As there's no _MouseMove event on the WS, use a timer.
Private Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos"
(lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As

Long,
ByVal yPoint As Long) As Long

But it would certainly be easier to just let Excel show the comments by
itself.

NickHK

"None" wrote in message
...
LOL I am writing a budgeting/Debt reduction program in excel with VBA.
I changed the time and date to test some code and forgot to chance it
back. Thanks for letting me know.

The Selction change event will not help, unless they select the cells.
This will not be allowed, as the cells I want to have comments for are
the column headers. The will be locked and unselectable.

Somehow excel must track where the mouse is, as it knows when to
display the in cell comments.

On Mon, 11 Dec 2006 13:06:11 +0800, "NickHK"
wrote:

You should fix your system clock as you are currently posting next

year.

But there is no mouse or "ShowComment" events on the worksheet.
What about using the _SelectionChange event ?

NickHK

"None" wrote in message
.. .
Anyone know of any "Mouse over cell events?" Here is what I want to
do:

I want to create my own custom cell comments. I will have a few cells
merged and refer to them as my Comment Text Cell. I want to be able

to
check if the mouse is moved over certain cells, and then have the
assigned text show up in my Comment Text Cell.