函数参考


ObjGet

对 COM 对象从现有的进程或文件名检索一个参考.

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

参数

文件名 包含对象的文件的完整路径和名称(参考备注).
类名 [可选参数] 类标识. Can be in either ProgID or the string representation of the CLSID.
instance [可选参数] Instance of the object for ROT objects of the same (co)class.

返回值

成功: 返回一个对象.
失败: 返回 0 并设置 @error.

注意/说明

如果你只想使用类名,文件名则是可选的,但参数不能被省略.
你可以使用一个空字符串来代替.你可以使用第三个参数来指定instance.

如果你使用一个文件名,类名则是可选的. 它只需要当多个类被定义到相同的文件类型,并且你指定了去访问一个特定的类.
保持警惕,不是所有计算机都拥有相同的对象集合. 因此 一定要 在调用 ObjCreate() 后检查错误.

参考 Obj/COM 参考 得到更多关于对象的信息.

相关

GUICtrlCreateObj, IsObj, ObjCreate, ObjEvent, ObjName

示例/演示


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

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

If @error Then
    MsgBox(4096, "ExcelTest", "Error Getting an active Excel Object. Error code: " & Hex(@error, 8))
    Exit
EndIf

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



; Example getting an Object using a file name
;
; An Excel file with filename Worksheet.xls must be created in the root directory
; of the C:\ drive in order for this example to work.

Local $FileName = "C:\Worksheet.xls"

If Not FileExists($FileName) Then
    MsgBox(4096, "Excel File Test", "Can't run this test, because you didn't create the Excel file " & $FileName)
    Exit
EndIf

Local $oExcelDoc = ObjGet($FileName) ; 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 $String = "" ; String for displaying purposes

    ; Some document properties do not return a value, we will ignore those.
    Local $OEvent = ObjEvent("AutoIt.Error", "nothing"); Equal to VBscript's On Error Resume Next

    For $Property In $oExcelDoc.BuiltinDocumentProperties
        $String = $String & $Property.Name & ":" & $Property.Value & @CRLF
    Next

    MsgBox(4096, "Excel File Test", "The document properties of " & $FileName & " are:" & @CRLF & @CRLF & $String)

    $oExcelDoc.Close ; Close the Excel document

Else
    MsgBox(4096, "Excel File Test", "Error: Could not open " & $FileName & " as an Excel Object.")
EndIf