Copy and Paste with VBA Excel in Google Chrome Internet Browser
The below code enables you to copy and paste at certain x,y coordinates on your internet browser with Excel VBA. You can effectively navigate the web and gather data by copying and pasting from the internet website to your Excel application.
Here are the references that I have installed in my Excel Visual Basic Editor Tools –> References. If you're unsure of how to access that information, then watch my Visual Basic Editor video for the basics on how to set-up VBA in Excel without downloads and navigate.
The below code snippet will help you to copy and paste words with your internet browser. It only works on Google Chrome and not 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
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
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()
SetCursorPos 350, 490 'x and y position
mouse_event mouseeventf_leftdown, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_MOVE, 150, 0, 0, 0
mouse_event mouseeventf_leftup, 0&, 0&, 0&, 0&
mouse_event mouseeventF_Rightdown, 0&, 0&, 0&, 0&
mouse_event mouseeventF_rightup, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_MOVE, 5, 5, 0, 0
mouse_event mouseeventf_leftdown, 0&, 0&, 0&, 0&
mouse_event mouseeventf_leftup, 0&, 0&, 0&, 0&
SetCursorPos 950, 100 'x and y position
mouse_event mouseeventf_leftdown, 0&, 0&, 0&, 0&
SetCursorPos 600, 520 'x and y position
mouse_event mouseeventF_Rightdown, 0&, 0&, 0&, 0&
mouse_event mouseeventF_rightup, 0&, 0&, 0&, 0&
SetCursorPos 610, 680 'x and y position
mouse_event mouseeventf_leftdown, 0&, 0&, 0&, 0&
mouse_event mouseeventf_leftup, 0&, 0&, 0&, 0&
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.
Declare Sub sleep Lib "kernel32" (ByVal dwmilliseconds As Long) and then Sleep 100 in the subprocedure makes your computer sleep for 100 milliseconds if you want to add a short wait in between instructions.
Application.Wait (Now + TimeValue("00:00:02")) also waits 2 seconds if you want to use this instead of the kernell32