Code for VBS to call Windows API functions

Source: Internet
Author: User

I accidentally found an article WinCC VBS using EXCEL to call Windows API functions that day. I don't know what WinCC is. Google seems to be related to SIEMENS automation. WinCC is not important. What matters is that this article provides an idea for VBS to call Windows APIs-excel vba, a language that is more legendary than VB.

However, the examples in that article use the excel vba program that has been written, that is, an EXCEL file must first exist. I was wondering if I could use the excel. application Object in VBS to create an EXCEL document containing VBA code and then call it? Google came to Google and finally found a solution in Microsoft's help and support.

Combine two articles (read these two articles first) and write an example program to move the mouse to the upper left corner of the desktop. If your EXCEL file is not too pirated, double-click the VBS file to see the effect.

Copy codeThe Code is as follows: Dim WshShell
Set WshShell = CreateObject ("wscript. Shell ")
WshShell. RegWrite "HKEY_CURRENT_USER \ Software \ Microsoft \ Office \ 11.0 \ Excel \ Security \ AccessVBOM", 1, "REG_DWORD"
WshShell. RegWrite "HKEY_CURRENT_USER \ Software \ Microsoft \ Office \ 12.0 \ Excel \ Security \ AccessVBOM", 1, "REG_DWORD"
WshShell. RegWrite "HKEY_CURRENT_USER \ Software \ Microsoft \ Office \ 14.0 \ Excel \ Security \ AccessVBOM", 1, "REG_DWORD"
Dim oExcel, oBook, oModule
Set oExcel = CreateObject ("excel. application ")
Set oBook = oExcel. Workbooks. Add
Set oModule = obook. VBProject. VBComponents. Add (1)
StrCode = _
"Private Declare Function SetCursorPos Lib" "user32" "(ByVal x As Long, ByVal y As Long) As Long" & vbCr &_
"Sub MyMacro (x as Long, y as Long)" & vbCr &_
"SetCursorPos x, y" & vbCr &_
"End Sub"
OModule. CodeModule. AddFromString strCode
OExcel. Run "MyMacro", 0, 0
OExcel. DisplayAlerts = False
OBook. Close
OExcel. Quit

The above 3-5 rows modify the Registry to allow VBS to fully control EXCEL. strCode is the VBA code written into EXCEL. As for how to call Windows API in VBA, it is not covered in this article, please check the information yourself. Use oExcel. Run "MyMacro", 0, 0 to call the VBA code we have written.
Original article: http://demon.tw/programming/vbs-excel-invoke-windows-api.html

Related Article

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.