Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control Mouse in VBA

Status
Not open for further replies.

Chacanger

Programmer
Feb 22, 2011
7
0
0
Hi

I've recently been trying to automate the mouse to do certian things in external programs as I can't actually automate them another way. I have tried to do some of this with Calculator recently but I can't seem to get the mouse to L click, hold the L click and R click.

I need to do the follwing steps so that I can use this as refference for a future idea I have in mind...

1.Open Calculator(C:\WINDOWS\system32\Calc.exe).
2.Use the mouse to move to 9 button and click it.
3.Use the mouse to move to + button and click it.
4.Use the mouse to move to 8 button and click it.
5.Use the mouse to move to = button and click it.
5.Use the mouse to move to the answer box and highlight the value ready to copy it.

Here is my code so far...

------------------------------------------------------------------

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Sub test()
Dim Rec As RECT

Shell "D:\WINDOWS\system32\Calc.exe", vbMaximizedFocus
GetWindowRect GetWindowHandle, Rec
SetCursorPos Rec.Right - 135, Rec.Top + 135
SetCursorPos Rec.Right - 200, Rec.Top + 135
SetCursorPos Rec.Right - 1100, Rec.Top + 166
End Sub

Private Function GetWindowHandle() As Long

Const CLASSNAME_MSExcel = "XLMAIN"

'Gets the Apps window handle, since you can't use App.hInstance in VBA (VB Only)
GetWindowHandle = FindWindow(CLASSNAME_MSExcel, vbNullString)
End Function
------------------------------------------------------------

It will need to be with mouse as there dosen't seem to be any other plausible way of doing it. Also I can't use automation software from were I'm situated

Many Thanks

Chacanger
 
I don't know the idea behind the project, but in pure vba you can (with reference to MSForms in first case):

- work with calculator using SendKeys:
Code:
Dim fDataObject As MSForms.DataObject
Set fDataObject = New MSForms.DataObject
Shell "Calc.exe", vbNormalFocus
SendKeys "9", True
SendKeys "{+}", True
SendKeys "8", True
SendKeys "=", True
SendKeys "^C", True
' close if necessary
SendKeys "%{F4}", True
' display result
fDataObject.GetFromClipboard
MsgBox fDataObject.GetText

- use automation for excel, in this case you can get hWnd directly from the Application object:
Code:
MsgBox Application.Hwnd

combo
 
You need to use the mouse_event functions.
Code:
Private Type POINTAPI
        x As Long
        y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private 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)

Private Const MOUSEEVENTF_ABSOLUTE = &H8000 '  absolute move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 '  left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 '  left button up
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20 '  middle button down
Private Const MOUSEEVENTF_MIDDLEUP = &H40 '  middle button up
Private Const MOUSEEVENTF_MOVE = &H1 '  mouse move
Private Const MOUSEEVENTF_RIGHTDOWN = &H8 '  right button down
Private Const MOUSEEVENTF_RIGHTUP = &H10 '  right button up

Public Sub Right_Click()
Dim MousePos As POINTAPI
Call GetCursorPos(MousePos)
mouse_event MOUSEEVENTF_RIGHTUP, MousePos.x, MousePos.y, 0, 0
End Sub
but you're probably better off using combo's send keys unless you're sure you can work out where to position the mouse!

hth

Ben

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
Thanks oharab & combo, I'll try these out and see if I can get my system to work with this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top