请教关于GetObject函数的用法。

xys1995 2006-10-17 08:54:53
GetObject("C:\DRAWINGS\SAMPLE.DRW", "FIGMENT.DRAWING")
GetObject("C:\DRAWINGS\SAMPLE.DRW")
GetObject("", "FIGMENT.DRAWING")
GetObject(, "FIGMENT.DRAWING")
GetObject("FIGMENT.DRAWING")

上面这个函数因为参数不同会有什么样不同的结果,麻烦大虾告知!
...全文
682 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZOU_SEAFARER 2006-10-17
  • 打赏
  • 举报
回复
再给你贴一个2楼的英文文章!
或许英文看起来更贴切的了解!


GetObject Function


Returns a reference to an object provided by an ActiveX component.

Syntax

GetObject([pathname] [, class])

The GetObject function syntax has these named arguments:

Part Description
pathname Optional; Variant (String). The full path and name of the file containing the object to retrieve. If pathname is omitted, class is required.
class Optional; Variant (String). A string representing the class of the object.


The class argument uses the syntax appname.objecttype and has these parts:

Part Description
appname Required; Variant (String). The name of the application providing the object.
objecttype Required; Variant (String). The type or class of object to create.


Remarks

Use the GetObject function to access an ActiveX object from a file and assign the object to an object variable. Use the Set statement to assign the object returned by GetObject to the object variable. For example:

Dim CADObject As Object
Set CADObject = GetObject("C:\CAD\SCHEMA.CAD")

When this code is executed, the application associated with the specified pathname is started and the object in the specified file is activated.

If pathname is a zero-length string (""), GetObject returns a new object instance of the specified type. If the pathname argument is omitted, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

Some applications allow you to activate part of a file. Add an exclamation point (!) to the end of the file name and follow it with a string that identifies the part of the file you want to activate. For information on how to create this string, see the documentation for the application that created the object.

For example, in a drawing application you might have multiple layers to a drawing stored in a file. You could use the following code to activate a layer within a drawing called SCHEMA.CAD:

Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")

If you don't specify the object's class, Automation determines the application to start and the object to activate, based on the file name you provide. Some files, however, may support more than one class of object. For example, a drawing might support three different types of objects: an Application object, a Drawing object, and a Toolbar object, all of which are part of the same file. To specify which object in a file you want to activate, use the optional class argument. For example:

Dim MyObject As Object
Set MyObject = GetObject("C:\DRAWINGS\SAMPLE.DRW", "FIGMENT.DRAWING")

In the example, FIGMENT is the name of a drawing application and DRAWING is one of the object types it supports.

Once an object is activated, you reference it in code using the object variable you defined. In the preceding example, you access properties and methods of the new object using the object variable MyObject. For example:

MyObject.Line 9, 90
MyObject.InsertText 9, 100, "Hello, world."
MyObject.SaveAs "C:\DRAWINGS\SAMPLE.DRW"

Note Use the GetObject function when there is a current instance of the object or if you want to create the object with a file already loaded. If there is no current instance, and you don't want the object started with a file loaded, use the CreateObject function.

If an object has registered itself as a single-instance object, only one instance of the object is created, no matter how many times CreateObject is executed. With a single-instance object, GetObject always returns the same instance when called with the zero-length string ("") syntax, and it causes an error if the pathname argument is omitted. You can't use GetObject to obtain a reference to a class created with Visual Basic.

ZOU_SEAFARER 2006-10-17
  • 打赏
  • 举报
回复
不用翻译了,上面有为兄弟比我快多了! 基本意思差不多,例子不同而已
ZOU_SEAFARER 2006-10-17
  • 打赏
  • 举报
回复
自己读读英文说明
Assigning an Object Reference Using GetObject
The GetObject function is most often used to assign a reference to an existing object, although you can also use it to assign a reference to a new object.

To assign a reference to an existing object, use the following syntax.

Set objectvariable = GetObject([pathname] [, progID])

The pathname argument can be the path to an existing file, an empty string, or omitted entirely. If it is omitted, then progID is required. Specifying the path to an existing file causes GetObject to create an object using the information stored in the file. Using an empty string for the first argument causes GetObject to act like CreateObject — it will create a new object of the class whose programmatic identifier is progID. The following table describes the results of using GetObject.

If the ActiveX component is running Result
Set X = GetObject(, "MySrvr.Application")
X references an existing Application object.
Set X = GetObject("", "MySrvr.Object") X references a new, externally creatable object.

If the ActiveX component is not running Result
Set X = GetObject(, "MySrvr.Object") An error is returned.
Set X = GetObject("", "MySrvr.Object") The ActiveX component (MySrvr) is started, and X references a new object.
一会给你翻译好贴给你!
pcwe2002 2006-10-17
  • 打赏
  • 举报
回复

GetObject 函数



返回文件中的 ActiveX 对象的引用。

语法

GetObject([pathname] [, class])

GetObject 函数的语法包含下面几个命名参数:

部分 描述
pathname 可选的;Variant (String)。包含待检索对象的文件的全路径和名称。如果省略 pathname,则 class 是必需的。
class 可选的;Variant (String)。代表该对象的类的字符串。


其中,class 参数的语法格式为 appname.objecttype,且语法的各个部分如下:

部分 描述
appname 必需的;Variant (String)。提供该对象的应用程序名称。
objecttype 必需的;Variant (String)。待创建对象的类型或类。


说明

使用 GetObject 函数可以访问文件中的 ActiveX 对象,而且可以将该对象赋给对象变量。可以使用 Set 语句将 GetObject 返回的对象赋给对象变量。例如:

Dim CADObject As Object
Set CADObject = GetObject("C:\CAD\SCHEMA.CAD")

当执行上述代码时,就会启动与指定的 pathname 相关联的应用程序,同时激活指定文件中的对象。

如果 pathname 是一个零长度的字符串 (""),则 GetObject 返回指定类型的新的对象实例。如果省略了 pathname 参数,则 GetObject 返回指定类型的当前活动的对象。如果当前没有指定类型的对象,就会出错。

有些应用程序允许只激活文件的一部分,其方法是在文件名后加上一个惊叹号 (!) 以及用于标识想要激活的文件部分的字符串。关于如何创建这种字符串的信息,请参阅有关应用程序创建对象的文档。

例如,在绘图应用程序中,一个存放在文件中的图可能有多层。可以使用下述代码来激活图中被称为 SCHEMA.CAD 的层:

Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")

如果不指定对象的 class,则自动化会根据所提供的文件名,来确定被启动的应用程序以及被激活的对象。不过,有些文件可能不止支持一种对象类。例如,图片可能支持三种不同类型的对象:Application 对象,Drawing 对象,以及 Toolbar 对象,所有这些都是同一个文件中的一部分。为了说明要具体激活文件中的哪种对象,就应使用这个可选的 class 参数。例如:

Dim MyObject As Object
Set MyObject = GetObject("C:\DRAWINGS\SAMPLE.DRW", "FIGMENT.DRAWING")

在上述例子中,FIGMENT 是一个绘图应用程序的名称,而 DRAWING 则是它支持的一种对象类型。

对象被激活之后,就可以在代码中使用所定义的对象变量来引用它。在前面的例子中,可以使用对象变量 MyObject 来访问这个新对象的属性和方法。例如:

MyObject.Line 9, 90
MyObject.InsertText 9, 100, "Hello, world."
MyObject.SaveAs "C:\DRAWINGS\SAMPLE.DRW"

注意 当对象当前已有实例,或要创建已加载的文件的对象时,就使用 GetObject 函数。如果对象当前还没有实例,或不想启动已加载文件的对象,则应使用 CreateObject 函数。

如果对象已注册为单个实例的对象,则不管执行多少次 CreateObject,都只能创建该对象的一个实例。若使用单个实例对象,当使用零长度字符串 ("") 语法调用时,GetObject 总是返回同一个实例,而若省略 pathname 参数,就会出错。不能使用 GetObject 来获取 Visual Basic 创建的类的引用。

863

社区成员

发帖
与我相关
我的任务
社区描述
VB COM/DCOM/COM+
c++ 技术论坛(原bbs)
社区管理员
  • COM/DCOM/COM+社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧