Is there any way to control the mouse with VBA. What I would like to do is make a tutorial and for example when a button is pushed the mouse will move to a menu on the Excel File Menu and click it to open it, then select one of the choices to demonstrate how to do it.
|Check out our Excel VBA Resources|
would open the File Open Dialog box. Most of the mouse commands have keyboard equivalents. Playing with the WIN32 API to move the mouse around can be very tedious and if you can find another way to accomplish the job I would encourage you to use it instead.
'**Win32 API Declarations
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
'**Win32 API User Defined Types
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
Private Sub test()
Dim Rec As RECT
'Get Left, Right, Top and Bottom of Form1
GetWindowRect GetWindowHandle, Rec
'Set Cursor position on X
SetCursorPos Rec.Right - 600, Rec.Top + 400
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)