Export (0) Print
Expand All

Interaction.GetObject Method

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

Namespace: Microsoft.VisualBasic
Assembly: Microsoft.VisualBasic (in microsoft.visualbasic.dll)

public static Object GetObject (
	[OptionalAttribute] string PathName,
	[OptionalAttribute] string Class
)
public static Object GetObject (
	/** @attribute OptionalAttribute() */ String PathName, 
	/** @attribute OptionalAttribute() */ String Class
)
public static function GetObject (
	PathName : String, 
	Class : String
) : Object
Not applicable.

Parameters

PathName

Optional. String. The full path and name of the file containing the object to retrieve. If PathName is omitted, Class is required.

Class

Required if PathName is not supplied. String. A string representing the class of the object. The Class argument has the following syntax and parts:

appname.objecttype

Parameter

Description

appname

Required. String. The name of the application providing the object.

objecttype

Required. String. The type or class of object to create.

Return Value

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

For more detailed information, see the Visual Basic topic GetObject Function (Visual Basic).

Use the GetObject function to load an instance of a COM component from a file. The following example illustrates this.

Dim CADObject As Object
CADObject = GetObject("C:\CAD\schema.cad")

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

Default Cases

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

Accessing a Subobject

Some applications allow you to activate a subobject associated with a file. To do this, 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.

layerObject = GetObject("C:\CAD\schema.cad!Layer3")

Specifying a Class

If you do not 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, can 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. The following example illustrates this.

Dim drawObj As Object
drawObj = GetObject("C:\Drawings\sample.drw", "Figment.Drawing")

In the preceding example, Figment is the name of a drawing application and Drawing is one of the object types it supports.

Using the Object

Once an object is activated, you refer to it in code using the object variable you declared. In the preceding example, you access properties and methods of the new object using the object variable drawObj. The following example illustrates this.

drawObj.Line(9, 90)
drawObj.InsertText(9, 100, "Hello, world.")
drawObj.SaveAs("C:\Drawings\sample.drw")
NoteNote:

Use the GetObject function when there is a current instance of the object or if you want to create the object with a file loaded. If there is no current instance, and you do not want the object started with a file loaded, use the CreateObject Function (Visual Basic).

If an object has registered itself as an ActiveX single-instance object, only one instance of the object is created, no matter how many times CreateObject is called. 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 cannot use GetObject to obtain a reference to a class created with Visual Basic.

Security noteSecurity Note:

The GetObject function requires unmanaged code permission, which might affect its execution in partial-trust situations. For more information, see SecurityPermission and Code Access Permissions.

The following example uses the GetObject function to obtain a reference to a specific Microsoft Excel worksheet (excelObj). It uses the worksheet's Application property to make Excel visible, to close it, and to perform other actions. Using two API calls, the detectExcel procedure looks for Excel, and if it is running, enters it in the Running Object table. The first call to GetObject causes an error if Excel is not already running, which in this example causes the excelWasNotRunning flag to be set to True. The second call to GetObject specifies a file to open. If Excel is not already running, the second call starts it and returns a reference to the worksheet represented by the specified file, test.xls. The file must exist in the specified location; otherwise, Visual Basic throws a FileNotFoundException. Next, the example code makes both Excel and the window containing the specified worksheet visible.

This example requires Option Strict Off because it uses late binding, where objects are assigned to variables of type Object. You can specify Option Strict On and declare objects of specific object types if you add a reference to the Excel type library from the COM tab of the Add Reference dialog box of the Project menu in Visual Studio.

' Add Option Strict Off to the top of your program.
Option Strict Off

' Test to see if a copy of Excel is already running.
Private Sub testExcelRunning()
    On Error Resume Next
    ' GetObject called without the first argument returns a
    ' reference to an instance of the application. If the
    ' application is not already running, an error occurs.
    Dim excelObj As Object = GetObject(, "Excel.Application")
    If Err.Number = 0 Then
        MsgBox("Excel is running")
    Else
        MsgBox("Excel is not running")
    End If
    Err.Clear()
    excelObj = Nothing
End Sub

Private Sub getExcel()
    Dim fileName As String = "c:\vb\test.xls"

    If Not My.Computer.FileSystem.FileExists(fileName) Then
        MsgBox(fileName & " does not exist")
        Exit Sub
    End If

    ' Set the object variable to refer to the file you want to use.
    Dim excelObj As Object = GetObject(fileName)
    ' Show Excel through its Application property. 
    excelObj.Application.Visible = True
    ' Show the window containing the file.
    Dim winCount As Integer = excelObj.Parent.Windows.Count()
    excelObj.Parent.Windows(winCount).Visible = True

    ' Insert additional code to manipulate the test.xls file here.
    ' ...

    excelObj = Nothing
End Sub

When you call the getExcel function, a check is made to see if Excel is already running. If it is not, then an instance is created.

Security noteSecurity Note:

For simplicity, the preceding example assumes that any window called XLMAIN belongs to an instance of Microsoft Excel. If another object, possibly launched by illicit tampering, created a window with that name, it would receive all the messages you intended for Excel. In an application to be used for production, you should include some more rigorous testing to verify that XLMAIN really belongs to Excel.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft