当前位置: 移动技术网 > IT编程>脚本编程>VBScript > VBS调用Windows API函数的代码

VBS调用Windows API函数的代码

2017年12月08日  | 移动技术网IT编程  | 我要评论

北京癫痫军海医院,狗仔英雄,索尼f700x

那天无意中搜索到一篇的文章,不知道wincc是什么,google了一下好像跟西门子自动化有关。wincc是什么并不重要,重要的是这篇文章提供了vbs调用windows api的一种思路——excel vba,一种传说比vb还要vb的语言。

但是那篇文章中的例子都是使用已经写好的excel vba程序,即首先得存在一个excel文件。我就想,能不能在vbs中通过excel.application对象创建一个包含vba代码的excel文档然后再调用它呢?google来google去,终于在中找到了方法。

结合两篇文章(请先阅读这两篇文章),写了一个示例程序,效果是移动鼠标至桌面左上角。如果你的excel不是太盗版,双击这个vbs后应该可以看到效果。

复制代码 代码如下:

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

前面3-5行的修改注册表是为了让vbs能够完全控制excel,strcode即为写入excel中的vba代码,至于怎样在vba中调用windows api不属于本文的讨论范围,请自己查阅资料。使用oexcel.run "mymacro",0,0调用我们写入的vba代码。
原文:http://demon.tw/programming/vbs-excel-invoke-windows-api.html

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网