曲则全,枉则直,洼则盈,敝则新,少则得,多则惑。 是以圣人抱一为天下式。不自见,故明;不自是,故彰;不自伐,故有功;不自矜,故长。

VBS小结

上一篇 / 下一篇  2009-09-22 19:59:21 / 个人分类:脚本语言

1.WScript与CScript的区别

编译器有两种WScript和CScript,WScript是windows窗体模式,而CScript是字符,命令行模式。如果不涉及输入和输出,两种模式没有区别,但是如果有,就会有区别。

比如wscript.echo "hello",WScript模式弹出窗口显示hello,而CSript是在命令行显示hello

2.CreateObject和GetObject

Createobject用的比较多了,可以创建对象,然后操纵此对象,包括文本,Excel,数据库,wshshell等。

无意中看到GetObject,这个函数可以取得位于硬盘的文件对象,或者已经写入内存的对象。

对于硬盘文件,如:

硬盘中有一个test的Excel文件,里面有个test的sheet,可以操纵此excel文件。

Dim MyTest
Set MyTest=GetObject("e:\test.xls")
MyTest.sheets("test").cells(24,2)="hi"
MyTest.save
Set MyTest=Nothing

而对于已写入内存的程序,先打开Excel后。

Set MyTest=GetObject(,"Excel.application")
MsgBox MyTest.cells(1,1)
Set MyTest=Nothing

感觉这里因为已经打开Excel中的sheet,所以取得的MyTest就是sheet对象,直接调用cells就可以了。

此外,getobject还有个用处就是可以通过GetObject操纵WMI。

3.Dictionary

VBS中有字典对象Dictionary,可以对应添加字段。

Set MyTest=CreateObject("scripting.dictionary")
MyTest.Add "key1","hello"
MyTest.Add "key2","world"
MsgBox MyTest.Item("key2")
Set MyTest=nothing

4.wsc文件

看到zzyybb112的wsc用法,受到提示,去找了下帮助,果然是有的。

wsc是windows script. component,可以从网上下载windows script. component Wizard,也可以自己写XML文件,帮助里有的。

<?xml version="1.0"?>
<component>

<registration
 description="MyTest"
 progid="MyTest.WSC"
 version="1.00"
 classid="{8f8bebc8-2966-457f-941d-f3b356d61798}"
>
</registration>

<public>
 <property name="hello">
  <get/>
  <put/>
 </property>
 <method name="print">
 </method>
 <event name="this is a test"/>
</public>

<implements type="Behavior" id="Behavior"/>

<script. language="VBScript">
<![CDATA[

dim hello

function get_hello()
 get_hello = hello
end function

function put_hello(newValue)
 hello = newValue
end function

function print()
 'print = "Temporary Value"
  put_hello(123)
  msgbox hello
end function

]]>
</script>

</component>
这里有个属性hello,然后在print方法里把hello值设为123,再打印出来。注册一下这个wsc文件后,就可以在VBS中调用了。

Set test=CreateObject("MyTest.wsc")
test.print
Set test=nothing

 


TAG: vbs VBS wsc

 

评分:0

我来说两句

日历

« 2024-03-24  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 46172
  • 日志数: 33
  • 文件数: 1
  • 建立时间: 2009-07-10
  • 更新时间: 2010-12-07

RSS订阅

Open Toolbar