VBA Code to Interact with Game

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
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top