📄 vbs115.htm
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML><HEAD><TITLE>GetObject Function</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="PRODUCT" CONTENT="Visual Basic Scripting Edition">
<META NAME="TECHNOLOGY" CONTENT="SCRIPTING">
<META NAME="CATEGORY" CONTENT="Language Reference">
<META NAME="Keywords" CONTENT="objects,;GetObject function,;object variable,;creating object,;objects, creating,;"><META NAME="Description" CONTENT="GetObject Function"></HEAD>
<BODY BGCOLOR=FFFFFF LINK=#0033CC>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->
<FONT FACE="Verdana, Arial, Helvetica" SIZE=2>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR VALIGN=TOP><TD WIDTH=360>
<FONT SIZE=1 COLOR=#660033>Microsoft® Visual Basic® Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>GetObject Function</B></FONT>
</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2> <A HREF="vbstoc.htm">Language Reference</A> <BR>
<A HREF="vbs16.htm">Version 2</A> <P></FONT>
</TD></TR>
</TABLE>
<FONT SIZE=2><P>
<A HREF="vbs116.htm">See Also</A></FONT>
<HR noshade SIZE=1>
<H5>Description</H5>
<BLOCKQUOTE>Returns a reference to an <a href="vbs0.htm#defOLEAutomationObject">Automation object</a> from a file.
</BLOCKQUOTE>
<H5>Syntax</H5>
<BLOCKQUOTE>
<b>GetObject(</b>[<i>pathname</i>] [<B>,</B> <i>class</i>]<b>)</b><P>
The <b>GetObject </b> function syntax has these parts:<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Part</b></FONT></TD>
<TD><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>pathname</i></FONT></TD>
<TD><FONT SIZE=2>Optional; String. Full path and name of the file containing the object to retrieve. If <i>pathname</i> is omitted, <i>class</i> is required.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>class</i></FONT></TD>
<TD><FONT SIZE=2>Optional; String. <a href="vbs0.htm#defClass">Class</a> of the object.</FONT></TD></TR>
</TABLE>
The <i>class</i> <a href="vbs0.htm#defArg">argument</a> uses the syntax <i>appname.objectype</i> and has these parts:<p>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Part</b></FONT></TD>
<TD><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>appname</i></FONT></TD>
<TD><FONT SIZE=2>Required; String. Name of the application providing the object.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>objectype</i></FONT></TD>
<TD><FONT SIZE=2>Required; String. Type or class of object to create.</FONT></TD></TR>
</TABLE>
</BLOCKQUOTE><P>
<H5>Remarks</H5>
<BLOCKQUOTE>
Use the <b>GetObject</b> function to access an Automation object from a file and assign the object to an object variable. Use the <b>Set</b> statement to assign the object returned by <b>GetObject</b> to the object variable. For example:<p>
<BLOCKQUOTE><PRE><FONT FACE="Courier" SIZE=3>Dim CADObject
Set CADObject = <FONT COLOR="#FF0000">GetObject(</FONT>"C:\CAD\SCHEMA.CAD"<FONT COLOR="#FF0000">)</FONT>
</FONT></PRE></BLOCKQUOTE>
When this code is executed, the application associated with the specified pathname is started and the object in the specified file is activated. If <I>pathname</I> is a zero-length string (""), <b>GetObject</b> returns a new object instance of the specified type. If the <I>pathname</I> argument is omitted, <b>GetObject</b> returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.<P>
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.<P>
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 <FONT FACE="Courier" SIZE=2>SCHEMA.CAD</FONT>:<p>
<BLOCKQUOTE><PRE><FONT FACE="Courier" SIZE=3>Set LayerObject = <FONT COLOR ="#FF0000">GetObject(</FONT>"C:\CAD\SCHEMA<FONT COLOR="#FF0000">.</FONT>CAD!Layer3"<FONT Color="#FF0000">)</FONT>
</FONT></PRE></BLOCKQUOTE>
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 <I>class</I> argument. For example:<p>
<BLOCKQUOTE><pre><FONT FACE="Courier" SIZE=3>Dim MyObject
Set MyObject = <FONT COLOR="#FF0000">GetObject(</FONT>"C:\DRAWINGS\SAMPLE.DRW", "FIGMENT.DRAWING"<FONT COLOR="#FF0000">)</FONT></FONT></pre></BLOCKQUOTE>
In the preceding example, <FONT FACE="Courier" SIZE=2>FIGMENT</FONT> is the name of a drawing application and <FONT FACE="Courier" SIZE=2>DRAWING</FONT> 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 <a href="vbs0.htm#defProperty">properties</a> and methods of the new object using the object variable <FONT FACE="Courier" SIZE=2>MyObject</FONT>. For example:
<BLOCKQUOTE><pre><FONT FACE="Courier" SIZE=3>MyObject.Line 9, 90
MyObject.InsertText 9, 100, "Hello, world."
MyObject.SaveAs "C:\DRAWINGS\SAMPLE.DRW"
</FONT></pre></BLOCKQUOTE>
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=87%><TR><TD COLSPAN=2 VALIGN=BOTTOM><hr noshade size=1></TD></TR>
<TR><TD VALIGN=TOP><FONT SIZE=2><b>Note</b> Use the <b>GetObject</b> 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 <b>CreateObject</b> function.</P>
If an object has registered itself as a single-instance object, only one instance of the object is created, no matter how many times <b>CreateObject</b> is executed. With a single-instance object, <b>GetObject</b> always returns the same instance when called with the zero-length string ("") syntax, and it causes an error if the <I>pathname</I> argument is omitted.
</FONT></TD></TR><TR><TD COLSPAN=2 VALIGN=TOP><hr noshade size=1></TD></TR></TABLE>
</BLOCKQUOTE>
<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">© 1997 by Microsoft Corporation. All rights reserved.</a></em></p>
</FONT></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -