Offhand1982
New Member
- Joined
- Dec 14, 2017
- Messages
- 2
I am trying to write VBA code to click a window and move the mouse back and forth for a game. What happens is the mouse selects the window and moves back and forth but does not actually move anything. It as if it wont recognize it is in the program. When I use it in paint/Excel any other program it works correctly. I am completely new to writing VBA and not sure how to get it to interact with windows outside of Microsoft products. This particular program I cannot find in reference tab either. Thanks in advance for your help. See Code:
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 CityscapeSkyline()
For k = 1 To 1
'Open MS Paint and select Natural pencil Brush with 6px width
SetCursorPos 800, 500
Sleep 50
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Sleep 2000
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
For i = 800 To 400 Step -1
For j = 500 To 500
SetCursorPos i, j
Sleep 10
Next j
Next i
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
SetCursorPos 400, 500
Sleep 50
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Sleep 2000
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
For g = 400 To 800 Step 1
For u = 500 To 500
SetCursorPos g, u
Sleep 10
Next u
Next g
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Next k
Application.SendKeys ("%{f4}")
Sleep 1000
SendKeys ("{ENTER}")
End Sub
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 CityscapeSkyline()
For k = 1 To 1
'Open MS Paint and select Natural pencil Brush with 6px width
SetCursorPos 800, 500
Sleep 50
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Sleep 2000
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
For i = 800 To 400 Step -1
For j = 500 To 500
SetCursorPos i, j
Sleep 10
Next j
Next i
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
SetCursorPos 400, 500
Sleep 50
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Sleep 2000
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
For g = 400 To 800 Step 1
For u = 500 To 500
SetCursorPos g, u
Sleep 10
Next u
Next g
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Next k
Application.SendKeys ("%{f4}")
Sleep 1000
SendKeys ("{ENTER}")
End Sub