编辑“︁
VBScript
”︁(章节)
跳转到导航
跳转到搜索
Template:Editnotice load/content
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
== 示范 == === Hello World === 最简单的例子: <syntaxhighlight lang="vb"> MsgBox "Hello World" </syntaxhighlight> 以<code>.vbs</code>文件保存。再使用<code>cscript.exe</code>或<code>wscript.exe</code>执行。 一个更复杂的例子中,示出了使用<code>MsgBox</code>作为函数(返回一个结果),并使用了三个参数,其中第二个参数使用的是常量。 <syntaxhighlight lang="vb"> Dim x ' These three produce the same result. However, the use of constants as in the third line ' is considered best practice. x = MsgBox("Hello World:Text",1+64+4096,"Hello World:Title") x = MsgBox("Hello World:Text",4161,"Hello World:Title") x = MsgBox("Hello World:Text", vbOKCancel+vbInformation+vbSystemModal, _ "Hello World:Title") ' Presents the number corresponding to the button pressed. Different constants will produce ' different behaviours. For example, vbOKCancel specifies two buttons in the dialogue box, ' whereas vbYesNoCancel specifies three. x = MsgBox("Hello World:Text", vbYesNoCancel+vbInformation,"Hello World:Title") MsgBox "The result is " & x </syntaxhighlight> === 终止任务 === VBScript能访问[[Windows管理规范]] (WMI),就像[[Windows任务管理器]]。以下的代码执行时将会终止(“杀掉”)任何关于''notepad.exe''的进程。 <syntaxhighlight lang="vb"> 'Terminate all processes involving the name <strProcessToKill> Option Explicit Dim strComputer, strProcessToKill, objWMIService, colProcess, objProcess strComputer = "." strProcessToKill = "notepad.exe" Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer _ & "\root\cimv2") Set colProcess = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = '" & strProcessToKill & "'") For Each objProcess in colProcess MsgBox "... terminating " & objProcess.Name objProcess.Terminate() Next</syntaxhighlight> 使用''Option Explicit''并不是必须的,但它被认为是VBScript的[[最佳实践]]。<ref>[http://msdn.microsoft.com/en-us/library/bw9t3484(VS.85).aspx Remarks on ''Option Explicit'' Statement] {{Wayback|url=http://msdn.microsoft.com/en-us/library/bw9t3484(VS.85).aspx |date=20160321175002 }}, MSDN Library</ref><ref>''Why force yourself to declare all variables?'', In VBScript For Dummies, John Walkenbach, IDG Books Worldwide, Inc., 1996</ref> === 创建具有唯一的名称的十个文件 === 这个实例显示如何创建文件并向它添加内容。它还演示了字符串连接。 <syntaxhighlight lang="vb"> For i = 1 to 10 createFile( i ) Next Public sub createFile(a) Dim fso,myFile filePath = "C:\file_name" & a & ".txt" Set fso=CreateObject("Scripting.FileSystemObject") Set MyFile= fso.CreateTextFile( filePath) MyFile.WriteLine("This is a separate file") MyFile.close End Sub </syntaxhighlight> === 发送按键 === SendKeys方法模拟一个或多个按键到活动窗口(模拟在键盘上输入)。 在该示例中,脚本发送字符串“Hello World!”3次,每次暂停2秒(2000毫秒)。SendKeys巨集可能会在某些程序中失效,因为一些软件(如在安装时输入许可证密钥)将检查是否是真正的按键,而不是虚拟的。 <syntaxhighlight lang="vb"> set shl = createobject("wscript.shell") shl.sendkeys "Hello World!" wscript.sleep 2000 shl.sendkeys "Hello World!" wscript.sleep 2000 shl.sendkeys "Hello World!" wscript.sleep 2000 </syntaxhighlight> 执行期间,“Hello World!”将显示在命令提示符。 === Windows文件操作 === 对象FileSystemObject执行一些文件操作(例如测试一个文件是否存在),并且还创建一个文本文件(一个TextStream对象)。 <syntaxhighlight lang="vb"> myfilename = "C:\Wikipedia - VBScript - Example - Hello World.txt" MakeHelloWorldFile myfilename Sub MakeHelloWorldFile (FileName) 'Create a new file in C: drive or overwrite existing file Set FSO = CreateObject("Scripting.FileSystemObject") If FSO.FileExists(FileName) Then Answer = MsgBox ("File " & FileName & " exists ... OK to overwrite?", vbOKCancel) 'If button selected is not OK, then quit now 'vbOK is a language constant If Answer <> vbOK Then Exit Sub Else 'Confirm OK to create Answer = MsgBox ("File " & FileName & " ... OK to create?", vbOKCancel) If Answer <> vbOK Then Exit Sub End If 'Create new file (or replace an existing file) Set FileObject = FSO.CreateTextFile (FileName) FileObject.WriteLine "Time ... " & Now() FileObject.WriteLine "Hello World" FileObject.Close() MsgBox "File " & FileName & " ... updated." End Sub </syntaxhighlight> <code>MakeHelloWorldFile</code>将会在按下按钮后于C:\ 驱动器根目录创建(若已经存在则更新)一个小文本文件。 === Excel对象操作 === <syntaxhighlight lang="vb"> Option Explicit '所有变量必须显式声明 Dim app,workbook,sheet Dim row,col Set app = WScript.CreateObject("Excel.Application") app.Visible = True Set workbook = app.WorkBooks.Add Set sheet = workbook.Worksheets(1) '10x10 random value For row = 1 To 10 For col = 1 To 10 sheet.Cells(row,col).Value = CInt(Int((100 * Rnd()) + 1)) Next Next Set sheet = workbook.Worksheets(2) '10x10 random value sheet.Range("A1:J10").Formula = "=Int(Rand() * 100 + 1)" </syntaxhighlight>
摘要:
请注意,所有对Local Chinese Wikipedia的贡献均可能会被其他贡献者编辑、修改或删除。如果您不希望您的文字作品被随意编辑,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源(详情请见
Project:著作权
)。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
导航菜单
个人工具
未登录
讨论
贡献
创建账号
登录
命名空间
页面
讨论
大陆简体
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
臺灣正體
查看
阅读
编辑
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息