Article ID: 113946
Article Last Modified on 7/1/2004
GetObject([pathname][,class])The pathname parameter can be the path to an existing file, "", or it can be left out. Specifying the path to an existing file causes GetObject to use an existing instance of the server application, which is either explicitly denoted in the second parameter or obtained from the registry based on the file's extension. The latter two syntax's are used to create new object references, with the added benefit over CreateObject that you can control instance usage for the server.
Syntax: Set X = GetObject(,"MySrvr.Object")
Result: X is reference to existing object
Syntax: Set X = GetObject("","MySrvr.Object")
Result: X is reference to new object
Syntax: Set X = GetObject(,"MySrvr.Object")
Result: Error 429
Syntax: Set X = GetObject("","MySrvr.Object")
Result: Server is started and X references new object
Sub Command1_Click ()
Dim fResult As Integer
fResult = SmartGetObject("Excel.Application")
End Sub
Function SmartGetObject (sClass As String) As Integer
Dim oTmpObject As Object
' If Server running, oTmpObject refers to that instance.
' If Server not running Error 429 is generated.
On Error Resume Next
Set oTmpObject = GetObject(, sClass)
' oTmpObject is reference to new object.
If Err = 429 Then
' Server not running, so create a new instance:
Set oTmpObject = GetObject("", sClass)
' NOTE: for Excel, you can add the next line to view the object
' oTmpObject.Visible = True
ElseIf Err > 0 Then
MsgBox Error$
SmartGetObject = False
Exit Function
End If
SmartGetObject = True
End Function
Keywords: kbhowto kbprogramming KB113946