Control the mouse with VBS, in EXCEL2010, 2013, 64-bit

Source: Internet
Author: User

Original Author article address: http://demon.tw/programming/vbs-control-mouse.html

Thanks to the original author's strategy, I learned to use VBS to control the mouse.

But the problem ensued, Excel2003 and Excel2007 environment, according to the article do absolutely no problem.

However, Excel2010 and Excel2013 can not be used, will pop-up window:

Error: Unable to run the "SETCURSORPOS" macro. This may be because the macro is not available in this workbook, or all macros are disabled.

Code: 800A03EC


Workaround:

Enable all macros in the macro settings, and check the custom Ribbon before developing the tool.

Then use the following code to resolve the issue.

Option Explicitdim Wshshelldim oexcel, obook, Omoduledim strRegKey, Strcode, x, Yset oexcel = CreateObject ("Excel.Applicat Ion ") ' Create an Excel object Set WshShell = CreateObject (" WScript. Shell ") strRegKey =" Hkey_current_user\software\microsoft\office\$\excel\security\accessvbom "StrRegKey = Replace ( strRegKey, "$", oexcel.version) WshShell.RegWrite strRegKey, 1, "REG_DWORD" Set obook = OExcel.Workbooks.Add ' Add Workbook set Omodule = obook. VBPROJECT.VBCOMPONENTS.ADD (1) ' Add Module Strcode = _ ' Private Type pointapi:x as long:y as Long:end type ' & VbCrLf &am P  _ "Private Declare ptrsafe Function setcursorpos Lib" "User32" "(ByVal x as Long, ByVal y as Long) as Long" & VbCrLf & _ "Private Declare ptrsafe Function getcursorpos Lib" "User32" "(Lppoint as Pointapi) as Long" & VbCrLf & _ " Private Declare ptrsafe Sub mouse_event Lib "" user32 "" Alias "" Mouse_event "" (ByVal dwFlags as Long, ByVal DX as Long, BYV Al Dy as Long, ByVal cbuttons as Long, ByVal dwExtraInfo as Long) "& VbCrLf & _"Public Function Getxcursorpos () as Long" & VbCrLf & _<span style= "White-space:pre" ></span> "Dim PT as Pointapi:getcursorpos Pt:getxcursorpos = pt. X "& VbCrLf & _" End function "& vbCrLf & _" Public Function Getycursorpos () as Long "& VbCrLf &am P _<span style= "White-space:pre" ></span> "Dim pt as Pointapi:getcursorpos Pt:getycursorpos = Pt. Y "& VbCrLf & _" End Function "& VbCrLf & _" Private Sub setcursor (x, y) "& vbCrLf & _ <span Styl E= "White-space:pre" ></span> "Setcursorpos x, y" & vbCrLf & _ "End Sub" oModule.CodeModule.AddFromString  Strcode ' Add VBA code in module ' Author:demon ' website:http://demon.tw ' date:2011/5/10x = Oexcel.run ("Getxcursorpos") ' Get mouse X coordinate y =       Oexcel.run ("Getycursorpos") ' Get mouse y coordinate wscript.echo x, Yoexcel.run ' SetCursor ', 30, 30 ' set mouse x y coordinate const mouseeventf_move = &h1const Mouseeventf_leftdown = &h2const Mouseeventf_leftup = &h4const Mouseeventf_riGhtdown = &h8const Mouseeventf_rightup = &h10const Mouseeventf_middledown = &h20const MOUSEEVENTF_MIDDLEUP = &h40const Mouseeventf_absolute = &h8000 ' simulates the left mouse button click oexcel. Run "Mouse_event", Mouseeventf_leftdown + mouseeventf_leftup, 0, 0, 0, 0 ' Simulate the left mouse button double-click (that is, fast two clicks) oexcel. Run "Mouse_event", Mouseeventf_leftdown + mouseeventf_leftup, 0, 0, 0, 0oexcel.run "mouse_event", Mouseeventf_leftdown + M Ouseeventf_leftup, 0, 0, 0, 0 ' simulate right-click oexcel. Run "Mouse_event", Mouseeventf_rightdown + mouseeventf_rightup, 0, 0, 0, 0 ' middle mouse click oexcel. Run "Mouse_event", Mouseeventf_middledown + mouseeventf_middleup, 0, 0, 0, 0 ' off exceloexcel.displayalerts = FalseoBook.Cl Oseoexcel.quit

What's new: I just added Ptrsafe to the original author's code after declare. In addition, a new function, setcursor, used to replace the original code of the Setcursorpos.

explanation of the problem: just because 64-bit Excel uses declare can be a mistake. In addition, if you do not use my new setcursor words, using setcursorpos will move the mouse to the top right of the screen, do not know why.

Ah ah ah ah, this problem annoyed me for a long time, I went to the VBS forum VBS Group asked, there is no result, I went to the VBA forum to ask, there is no result, the original author in the original article comments do not return me ah ah ah ah.

So.. I'm not going to be VBA at all, I'm starting to study VBA.


1. Run the following code in VBS with no errors. This indicates that the VBS call EXCEL2010 is not a problem.

2. The study understood a little bit of VBA,

Sub Tian () MsgBox "test if remote script can start", 0 + 64, "Test window" End Sub
By pressing ALT+F11 in Excel, you can open the VBA edit box and enter the above code to run successfully.

Then put it in a VBS and you can use it, which means it's not a problem with VBA.

3. This VBA code works correctly in Excel2003, and Excel2010 is not available.

Private Declare Function setcursorpos Lib "user32" (ByVal x as Long, ByVal y as Long) as Longprivate Declare Sub Mouse_eve NT Lib "user32" (ByVal dwFlags as Long, ByVal DX as long, ByVal dy as Long, ByVal cbuttons as Long, ByVal dwExtraInfo as L ONG) Private Const MOUSEEVENTF_LEFTDOWN = &h2private Const MOUSEEVENTF_LEFTUP = &h4private Sub Command1_Click () Setcursorpos, 500mouse_event mouseeventf_leftdown, 0, 0, 0, 0mouse_event mouseeventf_leftup, 0, 0, 0, 0End Sub
and Prompt for errors:

Compile error:

To use on a 64-bit system, you must update the code in this project. Check and update the Declare statements, and then mark them with the Ptrsafe property.


Seems to have finally found the problem! haha haha.

4. Check, although not very understand, in short, put Ptrsafe to declare behind it.

You can use it, and it doesn't matter if you put it in VBS.

Option Explicit Dim WshShell Dim oexcel, Obook, omodule Dim strRegKey, Strcode, x, y Set oexcel = CreateObject ("Excel.appl Ication ") ' Creates an Excel object Set WshShell = CreateObject (" WScript. Shell ") strRegKey =" Hkey_current_user\software\microsoft\office\$\excel\security\accessvbom "StrRegKey = Replace ( strRegKey, "$", oexcel.version) WshShell.RegWrite strRegKey, 1, "REG_DWORD" Set obook = OExcel.Workbooks.Add ' Add Workbook set OM Odule = obook. VBPROJECT.VBCOMPONENTS.ADD (1) ' Add Module Strcode = _ ' Private Declare ptrsafe Function setcursorpos Lib ' "user32" "(ByVal x as Long, ByVal y as Long) as Long "& VbCrLf & _" Private Declare ptrsafe Sub mouse_event Lib "" User32 "(ByVal Dwflag s as long, ByVal DX as long, ByVal dy as Long, ByVal cbuttons as Long, ByVal dwExtraInfo as Long) "& VbCrLf & _" P rivate Const Mouseeventf_leftdown = &h2 "& vbCrLf & _" Private Const Mouseeventf_leftup = &h4 "& VBCRL F & _ "Private Sub Command1_Click ()" & VbCrLf & _ "Setcursorpos 500, "& VbCrLf & _" Mouse_event mouseeventf_leftdown, 0, 0, 0, 0 "& VbCrLf & _" Mouse_event Mouseeventf_le Ftup, 0, 0, 0, 0 "& VbCrLf & _" End Sub "oModule.CodeModule.AddFromString strcode ' Add VBA code in module Oexcel.run" Comman  D1_click "' Close Excel oexcel.displayalerts = False obook.close oexcel.quit

5. Although the problem is solved, but after the original author's code declare after adding Ptrsafe, there is a problem, regardless of the setcursorpos set to what value, the mouse will only move to the upper right corner.

So, add the function setcursor, pass.


...


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Control the mouse with VBS, in EXCEL2010, 2013, 64-bit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.