Function Reference


ObjGet

Retrieves a reference to a COM object from an existing process or filename.

ObjGet ( "filename" [, "classname" [, instance]] )

Parameters

filename The full path and name to the file containing the object (See remarks).
classname [optional] Class identifier. Can be in either ProgID or the string representation of the CLSID.
instance [optional] Instance of the object for ROT objects of the same (co)class.

Return Value

Success: an object.
Failure: sets the @error flag to non-zero.

Remarks

The filename is optional if you want to use only the class identifier, but the parameter can't be omitted.
Use an empty string if you want to access objects from running objects table (ROT). You can use third parameter to specify wanted instance in that case.
In all other cases third parameter is ignored.

If you use a filename, the classname is optional. It's only required when you want to load an object of a specific class.
Classname parameter does not have default value. If it's specified then it's processed.

See the Obj/COM Reference for more information about Objects.

Related

GUICtrlCreateObj, IsObj, ObjCreate, ObjEvent, ObjName

Example

Example 1

#include <MsgBoxConstants.au3>

; Example getting an Object using it's class name
; ; Excel must be activated for this example to be successful

Example()

Func Example()
    Local $oExcel = ObjGet("", "Excel.Application") ; Get an existing Excel Object

    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "Excel File Test" & @CRLF & "Error Getting an active Excel Object. Error code: " & Hex(@error, 8))
        Return False
    EndIf

    $oExcel.Visible = 1 ; Let the guy show himself
    $oExcel.workbooks.add ; Add a new workbook
EndFunc   ;==>Example

Example 2

; Example getting an Object using a file name
; ; An Excel file with filename Worksheet.xls must be created in the script directory
; in order for this example to work.
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $sFileName = @ScriptDir & "\Worksheet.xls"

    If Not FileExists($sFileName) Then
        MsgBox($MB_SYSTEMMODAL, "", "Excel File Test" & @CRLF & "Can't run this test, because you didn't create the Excel file " & $sFileName)
        Return False
    EndIf

    Local $oExcelDoc = ObjGet($sFileName) ; Get an Excel Object from an existing filename

    If IsObj($oExcelDoc) Then

        ; Tip: Uncomment these lines to make Excel visible (credit: DaleHohm)
        ; $oExcelDoc.Windows(1).Visible = 1; Set the first worksheet in the workbook visible
        ; $oExcelDoc.Application.Visible = 1; Set the application visible (without this Excel will exit)

        Local $sString = "" ; String for displaying purposes

        For $oProperty In $oExcelDoc.BuiltinDocumentProperties
            $sString &= $oProperty.Name & ":" & $oProperty.Value & @CRLF
        Next

        MsgBox($MB_SYSTEMMODAL, "", "Excel File Test:" & @CRLF & "The document properties of " & $sFileName & " are:" & @CRLF & @CRLF & $sString)

        $oExcelDoc.Close ; Close the Excel document

    Else
        MsgBox($MB_SYSTEMMODAL, "", "Excel File Test" & @CRLF & "Error: Could not open " & $sFileName & " as an Excel Object.")
    EndIf
EndFunc   ;==>Example