Move Mouse - VBScript and Excel Macro
First of all, when you do this
Position = "+ 180"
Position = "- 180"
you're first setting Position
to be "+180" and then immediately overwriting it to be "-180". There's no point in doing that.
To answer your question more specifically, replace this part:
x & " " & Position & "," & y & " " & Position
with something like this:
x & "," & y
Prior to that you'll have to say what the new coordinates of the cursor should be, e.g.
x = "111"
y = "222"
Also, all of this you don't need and can delete:
GetMessagePos = Excel.ExecuteExcel4Macro( _
"CALL(""user32"",""GetMessagePos"",""J"")")
x = CLng("&H" & Right(Hex(GetMessagePos), 4))
y = CLng("&H" & Left(Hex(GetMessagePos), (Len(Hex(GetMessagePos)) - 4)))
That's where you get the old cursor position, and you don't need this since you say yourself you don't want to set a relative position.
All in all your script should look something like this:
Option Explicit
Dim Excel, x, y
Set Excel = WScript.CreateObject("Excel.Application")
x = "111"
y = "222"
Excel.ExecuteExcel4Macro ( _
"CALL(""user32"",""SetCursorPos"",""JJJ""," & x & "," & y & ")")
WScript.Sleep (100)
WScript.Echo "Program Ended"
Jimbo
Currently not open to new roles. Happy to chat about interesting leadership topics, though! LinkedIn: https://www.linkedin.com/in/james-mallison-a647694b/ Twitter: http://www.twitter.com/j7mbo Blog: https://blog.j7mbo.com Github: http://github.com/j7mbo
Updated on July 09, 2022Comments
-
Jimbo almost 2 years
Found a script that uses Excel to move the mouse in VBScript. This is almost exactly what I am looking for for my project, however I need to alter a small bit to enable me to enter coordinates in the code for it to move the mouse specifically to those coordinates; the code currently uses the current mouse position and moves from that position - I need to be able to have the mouse move to an ABSOLUTE position, not a RELATIVE one.
Here's the VBS code:
Option Explicit Dim Excel, GetMessagePos, x, y, Count, Position Set Excel = WScript.CreateObject("Excel.Application") GetMessagePos = Excel.ExecuteExcel4Macro( _ "CALL(""user32"",""GetMessagePos"",""J"")") x = CLng("&H" & Right(Hex(GetMessagePos), 4)) y = CLng("&H" & Left(Hex(GetMessagePos), (Len(Hex(GetMessagePos)) - 4))) Position = "+ 180" Position = "- 180" Excel.ExecuteExcel4Macro ( _ "CALL(""user32"",""SetCursorPos"",""JJJ""," & x & " " & Position & "," & y & " " & Position & ")") WScript.Sleep (100) WScript.Echo "Program Ended"
According to the code, the position of the mouse is moved from it's CURRENT POSITION +180 then -180. I need this code altered so I can put both the positions (in variable names X and Y) and use those to set the exact position of the mouse pointer.
I have messed around with the Excel.ExecuteExcel4Macro part and checked out the documentation for the function to no avail. Please can you help? Thanks :)
EDIT: I'm using VBScript because the system I am creating this on is VERY locked down. I have no way of using any other technologies bar this one, so help would be appreciated!