larryjfoster
New Member
- Joined
- Jul 19, 2017
- Messages
- 20
I found this in another thread at another site and could use it as well... the user posted a different problem that I too would like to resolve however I'm getting a run-time error before I can get to the question the original poster had and I can't figure out why.
I get
Run-time error '91':
Object variable or With block variable not set
at
pntCursor.X = testDiv.getBoundingClientRect().Left
Below is the code used, you can simply open an IE window and navigate to http://www.google.com then run the macro.
the end result would, using getBoundingClientRect to get the HTML element position and ClientToScreen to map that location to screen location and finally SetCursorPos to set the mouse position.
the original post where I copied the code is at Finding HTML element and moving cursor to element returning wrong position
I get
Run-time error '91':
Object variable or With block variable not set
at
pntCursor.X = testDiv.getBoundingClientRect().Left
Below is the code used, you can simply open an IE window and navigate to http://www.google.com then run the macro.
VBA Code:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Private Const MOUSEEVENTF_RIGHTUP As Long = &H10
Public Type POINTAPI
X As Long
Y As Long
End Type
Sub test()
Dim testDoc As HTMLDocument
Dim testDiv As HTMLDivElement
Dim ieWindow As InternetExplorer
Dim hwnd As Long
Dim pntCursor As POINTAPI
Dim pntRet As Long
Dim curPos As POINTAPI
For Each w In CreateObject("Shell.Application").Windows
With w
If .Name = "Internet Explorer" Then
Set ieWindow = w
Exit For
End If
End With
Next w
Set testDoc = ieWindow.document
Set testDiv = testDoc.getElementById("hplogo")
hwnd = 0
hwnd = FindWindow("IEFrame", vbNullString)
pntCursor.X = testDiv.getBoundingClientRect().Left
pntCursor.Y = testDiv.getBoundingClientRect().Top
pntRet = ClientToScreen(hwnd, pntCursor)
pntRet = SetCursorPos(pntCursor.X, pntCursor.Y)
SetCursorPosition = (pntRet <> 0)
mouse_event MOUSEEVENTF_RIGHTDOWN, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_RIGHTUP, 0&, 0&, 0&, 0&
End Sub
the end result would, using getBoundingClientRect to get the HTML element position and ClientToScreen to map that location to screen location and finally SetCursorPos to set the mouse position.
the original post where I copied the code is at Finding HTML element and moving cursor to element returning wrong position