`
ktc7000
  • 浏览: 33284 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

VBS基础知识

阅读更多
引用



一、VBS介绍
VB6.0分为VBS(脚本)和VBA(宏)
代码强度:VB6.0 > VBS > VBA
编程工具:PrimalScript. Enterprise

二、基本操作
Dim 定义变量
VB只有一种数据类型,但有多个子类型,变量在赋值时才会确定子类型

If语句
If...Then...Else...End If
If...Then...ElseIf...Then...Else...End If

Select Case语句
Select Case
  Case
  Case Else
End Select

循环语句
Do While 条件...Loop  条件为True时循环,语句执行次数>=0
Do...Loop While 条件  条件为True时循环,语句执行次数>=1
Do Until 条件...Loop  条件为False时循环,语句执行次数>=0
Do...Loop Until 条件  条件为False时循环,语句执行次数>=1
While...Wend
For i=0 To 10 (Step 2/-2)...Next  Step意为i每次递增2或递减2
For Each...in...Next 用于处理集合
数组下标从0开始,字符串从1开始
& 字符串连接符


三、常用函数
InputBox 输入
MsgBox 输出
Wscript.Echo 输出到Output面板
IsNumeric 判断是否整型
CStr 变量转成字符串型
CDble 变量转成双精度型
Split(str, ",") 把字符串按照分隔符进行分割,返回字符串数组
Mid(str,n,m) 返回字符串第n个位置的m个字符
UBound(arr) 返回数组长度
Len(str) 返回字符串长度
Join(arr,",") 返回用,连接数组元素的字符串
Rnd 返回0-1的随机数,与Randomize配合使用
Eval 计算一个表达式的值并返回结果
Round(str,n) 四舍五入保留小数点后n位
VarType 检查变量类型

四、数组
定义数组
1)Dim var(10)
2)Dim var
Redim var(10)

五、函数和过程
函数
Function 函数名()
End Function

过程
Sub 过程名()
End Sub

Function和Sub的区别:
Function执行后调用函数名可获得返回值,而Sub没有返回值
调用过程可用两种方式,当用第2种时应省略括号
1)Call 函数(参数1,参数2...)
2)函数 参数1,参数2...

六、传值ByVal和传地址ByRef
Dim a,b
a=1
b=2
SetValue a,b
WScript.Echo a&","&b
Sub setValue(ByVal c, ByRef d)
c=4
d=5
End Sub
输出结果:1,5
使用传址引用可修改变量的值,而传值引用不会修改变量的值
默认参数不加ByVal,则默认为ByRef

七、操作文件
定义句柄
Dim Var
Set Var=CreateObject()

1.1)读txt文件
Dim fso,file,str
Set fso=CreateObject("Scripting.FileSystemObject")
Set file=fso.OpenTextFile("E:\class\readtxt.txt")
Do Until file.AtEndOfStream
str=file.ReadLine
Loop
Set file=Nothing
Set fso=Nothing
1.2)写txt文件
Set file=fso.CreateTextFile("E:\class\writetxt.txt")
file.WriteLine "Hello!"

2.1)读excel文件
Dim xlsApp,wkBook,wkSheet,content
Set xlsApp=CreateObject("Excel.Application")
Set wkBook=xlsApp.Workbooks.Open("E:\class\readexcel.xlsx")
Set wkSheet=wkBook.Worksheets("Sheet1")
For i=1 To rowCount
content=wkSheet.cells(i,1)
Next
wkBook.Close
xlsApp.Quit
Set wkSheet=Nothing
Set wkBook=Nothing
Set xlsApp=Nothing

2.2)写excel文件
wkSheet.cells(i,5)=content
wkBook.Save

3.1)读DB
新建扩展名为.udl的文件,双击打开数据属性窗口设置OLE驱动和数据源
用记事本打开.udl文件,复制Provider这句话,作为strCnn
Dim Cnn,Rst,strCnn
Set Cnn=CreateObject("ADODB.Connection")
Set Rst=CreateObject("ADODB.Recordset")
strCnn="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=e:\class\calc.mdb;Persist Security Info=False"
Cnn.Open strCnn
Rst.Open "Select * from calc",Cnn
Rst.MoveFirst
Do While Not Rst.EOF
MsgBox Trim(Rst.Fields("TestNumber1"))
Rst.MoveNext
Loop
Rst.Close
Cnn.Close
Set Rst=Nothing
Set Cnn=Nothing

3.2)写DB
Set rstupdate=CreateObject("ADODB.Recordset")
rstupdate.open "update calc set decis='"&flag&"'", conn
rstupdate.Close
Set rstupdate=Nothing

4.1)读xml
Dim xmlDoc,xmlRoot,xmlChildItem,msg
Set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.load "E:\class\calc.xml"
Set xmlRoot=xmlDoc.documentElement
Set xmlChildItem=xmlRoot.firstChild
For Each xmlChildItem In xmlRoot.childNodes
Next
Set xmlChildItem=Nothing
Set xmlRoot=Nothing
Set xmlDoc=Nothing
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics