VBS calls the code for Windows API functions _vbs

Source: Internet
Author: User
Tags support microsoft

On that day, I stumbled upon an article called "WinCC VBS using Excel to invoke Windows API functions," and did not know what WinCC was, and Google seemed to have something to do with Siemens automation. WINCC is not important, it is important that this article provides a way for VBS to invoke the Windows API--excel VBA, a legend than VB also need VB language.

However, the examples in that article are the Excel VBA programs that have been written, that is, an Excel file is first available. I thought, could you create an Excel document that contains VBA code in the VBS by Excel.Application object and then call it? Google came to Google and finally found a way to help and support Microsoft.

Combining two articles (read both), write an example program that moves the mouse to the upper-left corner of the desktop. If your Excel is not too pirated, double click on this VBS should be able to see the effect.

Copy Code code 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 previous 3-5-line modification registry is designed to allow the VBS to fully control Excel,strcode is written to VBA code in Excel, and how to invoke the Windows API in VBA is outside the scope of this article, please consult your own data. Using Oexcel.run "MyMacro", 0,0 calls the VBA code we write.
Original: 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.