0 votes
in VBA by Beginner (5 points)

I want to automate clicking a button on Chrome browswer. This is the sequence of events I would like to do in Chrome Broswer:

  1. Click on an icon, I know the cursor position
  2. Wait for a few seconds
  3. Press the scroll bar, I know the cursor position
  4. Wait for a few seconds
  5. Press the keys "Alt+S" 
Would appreciate any help, examples or ways to do it, preferably in Excel VBA or other means.

1 Answer

0 votes
by Super Expert (2.4k points)
selected by
 
Best answer

Since you know the cursor positions, you can control the position and clicks of your mouse using this VBA Mouse Control tutorial. Your code should wind up looking something like this:

'Declare mouse events
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare 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)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10
'Declare sleep
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub MAIN()
SetCursorPos 16, 500 'change the coordinates to match your Chrome coordinates
Sleep 50
Call LeftClick 'click the mouse
Sleep 2000 'wait 2 seconds
SetCursorPos 250, 2500 'change the coordinates
Sleep 50
Call LeftClick 'click the mouse again
Sleep 2000 'wait 2 seconds
SendKeys "%s" ' Press Alt+S
End Sub

Private Sub LeftClick()
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  Sleep 50
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Once you update the coordinates and the pauses (in milliseconds), you would run the module named MAIN.

I hope this helps point you in the right direction!

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...