1. VBScript变量
  • 变量定义的修饰符
  • 定义一个变量时可以暂时不用初始化
  • Option Explicit
  • 变量的类型
  • 其他知识
  1. VBScript子程序和函数
  • 子程序
  • 函数
  1. VBScript条件和循环语句
  • 条件语句
  • 循环语句
  1. VBScript关键字
  • VBScript脚本中有哪些关键字

VBScript变量

  1. 变量定义的修饰符
  • dim/public/private 通常使用dim来作为变量的修饰符
  1. 定义一个变量时可以暂时不用初始化
    eg: dim a

注意:也可以不适用dim来声明变量,变量可以直接通过自身赋值来使用eg: a=1dim a a=1的效果是一样的

  1. Option Explicit
  • 由于不需要声明变量,变量据可以直接使用,所以有时候再进行书写变量时,如果写错了,则系统不会报错,而是会新建一个变量,所以使用Option Explicit它可以有效的避免这种情况的发生,因为使用它后,整个脚本里的变量必须要先声明才能够使用,否则就会报错
  • Option Explicit如何使用?
    直接再脚本的开头写上这段代码即可,建议在书写每个vbs脚本的时候,都因该先写上这段代码
  1. 变量的类型
    变量可以有很多的类型,可以是一个数字型、字符串、或者是对象类型的
    因为在vbs脚本在运行时,系统会自动解析变量的类型,所以我们不需熬
    为变量的类型操心
  2. 其他知识
  • msgbox() 这个函数表示显示一个对话框:
  1. 第一个参数是这个对话框里面的内容
  2. 第二个参数表示的是这个对话框的标题
  3. 第三个参数表示的是这个对话框的烈性
  • inp0utbox() 这个函数表示显示一个输入的对话框
    里面有一个参数表示的是这个输入框输入内容的标题

示例代码:

Option Explicit
dim first,seconds,fuhao,result
first=int(inputbox("请输入第一个数"))
fuhao=inputbox("请输入算术表达式的符号")
seconds=int(inputbox("请输入第二个数"))
if first="" then
    msgbox("第一个输入框输入的值不能为空")
elseif fuhao="" then
    msgbox("符号不能为空")
elseif seconds="" then
    msgbox("第三个数不能为空")
else
    if fuhao="+" then result=first+seconds
    if fuhao="-" then result=first-seconds
    if fuhao="*" then result=first*seconds
    if fuhao="/" then result=first/seconds
end if

子程序和函数

  1. 子程序
  • 使用方法
sub subname(argument1,argument2,...)
    sub code
end sub
  • 注意: sub子程序没有返回值

示例代码:

dim sum
sub mysub(a,b)
    sum=a+b
    msgbox(sum)
end sub
mysub(1,2)
  1. 函数
  • 使用方法
function functionname(argument1,argument2,...)
    function code
end function

示例代码

dim result
function myfunction(a,b)
    function=a+b
end function
result=function(1,2)
msgbox(result)
  1. 子程序和函数的区别

子程序没有返回值,而函数有
子程序不能在表达式中使用,而函数可以

VBScript条件语句和循环语句

  1. 条件语句
  1. if...then
dim age
age=inputbox("请输入学生的年龄")
if age<0 or age>100 then msgbox("请输入正常的年龄")
if age>0 and age<18 then msgbox("未成年,不能谈恋爱")
if age>18 then msgbox("成年了,可以谈一场轰轰烈烈的恋爱了")

注意这里的逻辑运算符使用的是andor

  1. if...then...else...end if
dim score
score=inputbox("请输入学生的成绩")
if score<0 or score>100 then
msgbox("录入分数失败,格式不符合要求")
elseif score>=60 or score<70 then
msgbox("分数及格")
elseif score>=70 and score<80 then
msgbox("分数良好")
elseif score>=80 then
msgbox("分数优秀")
end if
  1. if...then...elseif...else...end if
dim first,seconds,fuhao,result
first=int(inputbox("请输入第一个数"))
fuhao=inputbox("请输入算术表达式的符号")
seconds=int(inputbox("请输入第二个数"))
if first="" then
msgbox("第一个输入框输入的值不能为空")
elseif fuhao="" then
msgbox("符号不能为空")
elseif seconds="" then
msgbox("第三个数不能为空")
else
    if fuhao="+" then result=first+seconds
    if fuhao="-" then result=first-seconds
    if fuhao="*" then result=first*seconds
    if fuhao="/" then result=first/seconds
end if
  1. select case
dim month
month=inputbox("请输入月份")
select case month
    case ""
        msgbox("输入不能为空")
    case 1
        msgbox("一月份")
    case 2
        msgbox("二月份")
    case 3
        msgbox("三月份")
    case 4
        msgbox("四月份")
    case 5
        msgbox("五月份")
    case 6
        msgbox("六月份")
    case 7
        msgbox("七月份")
    case 8
        msgbox("八月份")
    case 9
        msgbox("九月份")
    case 10
        msgbox("十月份")
    case 11
        msgbox("十一月份")
    case 12
        msgbox("十二月份")
    else case
        msgbox("月份输入错误")
    end select
  1. 循环语句
  1. for...next
REM 计算1+2+3+...+100的和
' 使用for循环
dim count,sum
sum=0
for count=1 to 100
    sum=sum+count
next
msgbox("1+2+3+...+100="&sum)

for循环可以嵌套使用,还可以和其他条件语句一起使用

示例代码1

' 使用递增计算1+3+5+...+99的和
dim count,sum
sum=0
for count=1 to 99 step 2
    sum=sum+count
next
msgbox("1+3+5+...+99="&sum)

示例代码2

' 使用递减计算1+3+5+...+99的和
dim count,sum
sum=0
for count=99 to 1 step -2
    sum=sum+count
next
msgbox("1+3+5+...+99="&sum)

示例代码3

' for驯悍和if嵌套使用
dim count
for count=1 to 100
    if count=50 then
        msgbox("执行完成了")
        Exit for
    end if
next
msgbox("当count="&count&"时,退出for循环")
  1. do...loop  2.2.1 do...loop
REM 这是一个死循环,切忌使用此方法时不加任何结束判断
dim count
do
    count=count+1
    msgbox(count)
loop

 2.2.2 do while...loop

' 使用do...loop while方法
' 先判断while里面的值是否满足条件,满足则执行里面的语句
dim count
count=10
do while count=10
    msgbox(count)
loop
msgbox("执行完成了")

 2.2.3 do...loop while

' 使用do...loop while方法
' 无论如何do...loop while都会先至少执行一次
dim count
count=9
do
    msgbox(count)
    count=count+1
loop while count=10

 2.2.4 do Until...loop

' 使用do Until...loop
' 直到妈祖Until后的条件才结束do Until...loop循环
dim count
count=9
do Until count=10
    msgbox(count)
    count=count+1
loop
msgbox("执行结束了")

 2.2.5 do...loop Until

' 使用do...loop Until方法
' 无论如何,do...loop Until至少执行一次循环
dim count
count=9
do
    msgbox(count)
    count=count+1
loop Until count=10
msgbox("执行结束了")

VBScript关键字

  1. VBScript脚本中有哪些关键字
  1. Empty
  • 判断方法:IsEmpty()示例代码:
dim a
if IsEmpty(a) then msgbox("a没有初始化")
  1. Null
  • 判断方法: IsNull()示例代码:
dim a
a=Null
if IsNull(a) then msgbox("a不是一个有效数据")
  1. Nothing
  • 判断方法: Is Nothing
Set obj=Nothing
if obj Is Nothing then msgbox("obj对象没有初始化值")
  1. True
  2. False