如何在 VBScript 中使用 GetObject [英] How to use GetObject in VBScript

查看:28
本文介绍了如何在 VBScript 中使用 GetObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是 VBScript 的新手.所以这是我的代码,它读取一个文本文件并将一个对象附加到它:

I am really new to VBScript. So this is my code which reates a text file and attaches an object to it:

Set objExcel = CreateObject("Scripting.FileSystemObject")
objExcel.CreateTextFile("C:\mine.txt")

现在谁能告诉我应该如何使用 getObject(Pathname,[class]) 函数,因为我尝试了很多但没有任何效果?感谢和问候

Now can anyone please tell me how I am supposed to use the getObject(Pathname,[class]) function, as I was trying a lot but nothing worked out well? Thanks and regards

推荐答案

VBScript GetObject 文档可以在 此处.这是一个 VBScript 示例:

VBScript GetObject documentation can be found here. Here is a VBScript sample:

Set objExcelFile = GetObject("C:\Scripts\Test.xls")
WScript.Echo objExcelFile.Name
objExcelFile.Close

此代码将为您提供包含在 C:\Scripts\Test.xls 中的 Excel 工作簿对象.您可以使用 TypeName() 来确认:

This code will get you the Excel Workbook object contained in C:\Scripts\Test.xls. You can use TypeName() to confirm that:

Set objExcelFile = GetObject("C:\Scripts\Test.xls")
WScript.Echo objExcelFile.Name
WScript.Echo TypeName(objExcelFile)
objExcelFile.Close

输出将是:

test.xls
Workbook

如果指定的 Excel 工作簿不存在,脚本将返回错误.如果 Excel 实例已在运行,您可以使用此代码获取对它的引用:

If the specified Excel workbook doesn't exist the script will return an error. If an instance of Excel is already running you can use this code to get a reference to it:

Set objExcel = GetObject( , "Excel.Application")

For Each objWorkbook In objExcel.Workbooks
    WScript.Echo objWorkbook.Name
Next

objExcel.Quit

注意Excel.Application"前的逗号.该脚本获取对正在运行的 Excel 应用程序的引用,列出打开的工作簿名称并退出 Excel.如果没有运行 Excel 实例,您将收到错误消息.这也是您不能使用 GetObject() 获取 Scripting.FileSystemObject 实例的原因 - 没有正在运行的实例.

Notice the comma before 'Excel.Application'. The script gets a reference to a running Excel application, lists open workbooks names and quits Excel. If there is no Excel instance running you will receive an error. This is also why you couldn't use GetObject() to get an instance of Scripting.FileSystemObject - there was no running instance.

如果内存中已经有一个 Scripting.FileSystemObject 实例,您可以使用 GetObject.试试这个代码:

You could use GetObject if there is an instance og Scripting.FileSystemObject already running in memory. Try this code:

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFso1 = GetObject("", "Scripting.FileSystemObject")

WScript.Echo TypeName(objFso)
WScript.Echo TypeName(objFso1)

你可以看到你首先需要使用 CreateObject() 并且当 FileSystemObject 运行时可以获得对它的引用,但这没有多大意义,因为你已经有了对它的引用 (objFso).因此,使用 CreateObject() 创建 FileSystemObject 的实例.

You can see that you first need to use CreateObject() to and when FileSystemObject is running it is possible to get a reference to it, but this doesn't make much sense because you already have a reference to it (objFso). So, use CreateObject() to create an instance of FileSystemObject.

这篇关于如何在 VBScript 中使用 GetObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆