VBA Click Internet Explorer with Mouse

The below code lets you click on specific parts of Internet Explorer or Google Chrome. I have other articles under the below code to cover topics such as planning out a series of XY coordinate clicks on your PC monitor, positioning your browser location, waiting until internet explorer loads, waiting a certain amount of time, copy and pasting information from and to internet browsers, etc.

Here are the references that I have installed in Excel so that this can work. If you're unsure of how to install references, then I recommend you watch my Visual Basic Editor Video in addition to the Main VBA Excel lessons on my Youtube.

Code to click around Internet Explorer:


Private Declare Sub mouse_event Lib "user32" _
(ByVal dwFlags As Long, ByVal dx As Long, _
ByVal dy As Long, ByVal cButtons As Long, _
ByVal swextrainfo As Long)
Private Const mouseeventf_leftdown = &H2
Private Const mouseeventf_leftup = &H4
Private Const mouseeventF_Rightdown As Long = &H8
Private Const mouseeventF_rightup As Long = &H10
Public Declare Function SetCursorPos Lib "User32.dll" _
(ByVal X As Integer, ByVal y As Integer) As Long


Declare Sub sleep Lib "kernel32" (ByVal dwmilliseconds As Long)

Public Declare Function GetCursorPos Lib "user32" _
(lpPoint As PointAPI) As Long
Public Type PointAPI
X As Long
y As Long
End Type

Sub aaa()

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

With IE
.Visible = True
.Navigate2 "google.com"
End With
Application.Wait (Now + TimeValue("00:00:03"))


SetCursorPos 350, 490 'x and y position sets it
mouse_event mouseeventf_leftdown, 0&, 0&, 0&, 0&
mouse_event mouseeventf_leftup, 0&, 0&, 0&, 0&


End Sub

SetCursorPos sets the mouse position on your screen. The first number is X coordinate left to right. The second number is Y coordinate up to down.

mouse_event mouseeventf_leftdown, 0&, 0&, 0&, 0& is the mouse left click but only the down portion of it.

mouse_event mouseeventf_leftup, 0&, 0&, 0&, 0& is the mouse left click but only the up portion of it

mouse_event mouseeventF_Rightdown, 0&, 0&, 0&, 0&
mouse_event mouseeventF_rightup, 0&, 0&, 0&, 0&
same as above but right click

mouse_event MOUSEEVENTF_MOVE, 150, 0, 0, 0 moves the mouse to the right for the first number and down for the second number. I don't know or care what the other numbers do.

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
creates Internet Explorer object on your PC.

With IE
.Visible = True
.Navigate2 "google.com"
End With
makes internet explorer visible and goes to the website wallstbets.com, the #1 best financial news website in U.S.A.

Application.Wait (Now + TimeValue("00:00:02")) waits 2 seconds if you want to use this instead of the kernell32


Click the below image to learn how to control the mouse even more with Excel VBA. You can plan a series of clicks with this program. I built a 2016 and a 2019 version.

Click the below image to learn how to webscrape with excel VBA:


If you want to learn how to set the browser location or size watch the below video:


If you want other code I don't really care you can copy it from the video or from below not organized in any way. This just goes to Google and loops through the HTMLInputElements and fills in a word. Kind of pointless when you can paste. The bottom of my page puts excel in a certain spot.

The below code creates IE in a particular spot:

Sub blahblah()

Set objE = CreateObject("InternetExplorer.Application")
With objE
.navigate "www.google.com

.top = -1080
.left = 3840
.height = 500
.width = 1920
.visible = true
end with

End Sub

The below code creates a Google Chrome browser. If you don't know where to find your Google Chrome browser I show you in my Multiple XY coordinates youtube video above:

'task manager right click and open file location to get where the .exe is
Shell ("C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe -url http:google.com")

SetCursorPos 20, 60 'x and y position
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
    

Here's random useless code for my own reference.

Private Declare Sub mouse_event Lib "user32" _
(ByVal dwFlags As Long, ByVal dx As Long, _
ByVal dy As Long, ByVal cButtons As Long, _
ByVal swextrainfo As Long)
Private Const mouseeventf_leftdown = &H2
Private Const mouseeventf_leftup = &H4
Private Const mouseeventF_Rightdown As Long = &H8
Private Const mouseeventF_rightup As Long = &H10
Public Declare Function SetCursorPos Lib "User32.dll" _
(ByVal X As Integer, ByVal y As Integer) As Long


Declare Sub sleep Lib "kernel32" (ByVal dwmilliseconds As Long)

Public Declare Function GetCursorPos Lib "user32" _
(lpPoint As PointAPI) As Long
Public Type PointAPI
X As Long
y As Long
End Type

Sub aaa()
Dim form As Variant
Dim button As Variant
   Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
Dim counter As Double
counter = 1
    With IE
        .Visible = True
        .Navigate2 "Google.com"
    End With
  For Each itm In IE.Document.all
       If counter > 150 Then
       GoTo line100:
       End If
        If itm = "[object HTMLInputElement]" Then
        
                itm.Value = "Apple"
        
            End If
            counter = counter + 1
      
Next
line100:
 
 Set form = IE.Document.getElementsByTagName("form")
 Set button = form(0).onsubmit
 form(0).submit

Comments are closed.