Finding HTML element and moving cursor to element returning wrong position

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.

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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi - sorry it's taken so long to respond to this. Have you managed to solve it or do you still need help?
I had a quick look, and I managed to solve the error you're experiencing, but there is another problem later on down the code that it would be helpful to ask you about.
 
Upvote 0
Hi - sorry it's taken so long to respond to this. Have you managed to solve it or do you still need help?
I had a quick look, and I managed to solve the error you're experiencing, but there is another problem later on down the code that it would be helpful to ask you about.
Hi! thank you so much for taking a look. It is still a problem I have been unable to solve and would love any feedback.
 
Upvote 0
Your code doesn't appear to successfully assign anything to the testdiv variable - this is because the variable has been declared to be an HTMLDivElement, but looking at the HTML code for the Google website, the element with the hplogo ID is not a DIV, it's an IMG. I wasn't able to check the appropriate data type at the time (HTMLImg?) but instead tested it with the generic Object data type. This worked, but it then threw an error on the line:
VBA Code:
pntCursor.X = testDiv.getBoundingClientRect().Left
It was not able to access the getBoundingClientRect function/method to get the coordinates of the image element. Looking through the available properties of the element, there are similar-looking top/left coordinate properties, but I thought it was best first to check with you to see what it was you were actually after, because I'm guessing you're not really the top left coordinates of the Google logo, are you?
 
Upvote 0

Forum statistics

Threads
1,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top