常用vbs语法

mac2024-03-12  22

vbs (脚本语言)

VBS是基于Visual Basic的脚本语言。VBS的全称是:Microsoft Visual Basic Script Edition。(微软公司可视化BASIC脚本版)。其语言类似Visual Basic(VB)。

语句:Option Explicit    强制要求显式声明脚本中的所有变量。 说明:该语句应该在脚本所有语句之前,使用该语句后变量必须先声明后使用,否则会报错。建议使用该语句,可避免拼错变量名称等错误。  

函数或语句描述实例Dim 语句声明变量并分配存储空间。Dim x,y  '声明x、y两个变量。Dim username(2)  '声明一个具有 3个元素的数组。Const 语句声明用于代替文字值的常数。Const vbPI=3.14Const conPI=3.14’习惯用vb或con当前缀,便于区分InputBox显示一个输入框yourname=InputBox(“Enter your name:”)MsgBox显示一个消息框(对话框)MsgBox “hello world”UBound返回数组的最大下标arraybound=UBound(username)LCase把指定的字符串转换为小写x=LCase(“aBc”)UCase把指定的字符串转换为大写x=UCase(“Abc”)StrComp比较两个字符串,并返回一个值:相等返回0x>y返回1x<y返回-1x或y为Null返回NullStrComp(username,inun) 'StrComp 如果字符相同则返回结果为0,否则为非0Len返回字符串中字符的数目Len(x)Replace用另一个字符串替换字符串的指定部分x=Replace(“hello,zhangsan”,“zhangsan”,“lisi”)Split分割字符串为数组x=“zhangsan,lisi"username=Split(x,”,")CBool(expression)表达式结果为0返回False,否则返回TrueCBool(x=y)CInt把表达式转换为整数类型CInt(4.3)CDate将有效的日期和时间表达式转换为Date 类型CDate(“12/12/2012”)Asc返回对应的ANSI 字符代码Asc(“A”)Chr把指定的 ANSI 字符代码转换为字符Chr(65)Date返回当前的系统日期DateTime返回当前的系统时间TimeNowNow = Date + TimeNowDay返回日期中的"日"Day(Date)Month返回日期中的"月"Month(Date)Year返回日期中的"年"Year(Date)Hour返回时间中的"时"Hour(Time)Minute返回时间中的"分"Minute(time)Second返回时间中的"秒"Second(Time)Weekday返回该日期是星期几,默认星期天为第一天返回1Weekday(Date)

条件语句

If 语句

If xxx Then xxx End If If xxx Then xxx Else If xxx Then xxx End If End If If xxx Then xxx ElseIf xxx Then xxx Else xxx End If

Select Case语句

Select Case xxx Case 1 xxx Case 2 xxx Case 3 xxx Case Else xxx End Select

循环语句

Do While i<101 xxx i=i+1 If i>10 Then Exit Do '退出循环 End if Loop For i=1 To 100 xxx Next

函数过程

'有返回值 Function addtest(x,y) addtest=CInt(x)+CInt(y) End Function '无返回值 Sub subtracttest(x,y) subtracttest=CInt(x)-CInt(y) End Sub

文件夹操作

Sub createfolder(folderpath,foldername) Dim fso,f Set fso=CreateObject("Scripting.FileSystemObject") If fso.FolderExists(folderpath&foldername) Then MsgBox "目录已存在" Else MsgBox "目录不存在,现在开始创建..." Set f=fso.CreateFolder(folderpath&foldername) End If Set fso=Nothing End Sub createfolder "c:\","fsotest"

文件操作

Sub writelog(filepath,logstr) Const ForReading=1,ForWritting=2,ForAppdening=8 Dim fso,f Dim teststr Set fso=CreateObject("scripting.filesystemobject") If fso.FileExists(filepath) Then MsgBox "Exist" Else MsgBox "Not Exist" Set f=fso.CreateTextFile(filepath) End If Set f=fso.OpenTextFile(filepath,ForAppdening,false) f.WriteLine logstr f.Close Set f=fso.OpenTextFile(filepath,ForReading,False) teststr=f.ReadLine MsgBox teststr f.Close Set fso=Nothing End Sub writelog "c:\test.txt",Now()&"---执行自动化测试"

定义excel对象

'定义excel对象 Dim exlapp '定义excel工作薄 Dim exlworkbook '定义excel工作表 Dim exlworksheet '创建一个excel对象 Dim fso Dim exlexist Set exlapp=CreateObject("Excel.application") Set fso=CreateObject("scripting.filesystemobject") exlexist=fso.FileExists("c:\test.xls") If exlexist Then Set exlworkbook=exlapp.Workbooks.Open("c:\test.xls") Else Set exlworkbook=exlapp.Workbooks.Add End If Set exlworksheet= exlworkbook.Worksheets.Add exlworksheet.cells(1,1)="Hello,Test" '保存excel文件,如果原来存在,则使用save方法进行保存, '如果是新建,则使用saveas If exlexist Then MsgBox "文件存在" exlworkbook.Save Else MsgBox "文件不存在" exlworkbook.SaveAs "c:\test.xls" End If '关闭工作薄对象 exlworkbook.Close '退出excel对象 exlapp.Quit '释放销毁相关资源 Set exlworksheet=Nothing Set exlworkbook=Nothing Set exlapp=Nothing
最新回复(0)