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