对 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. |
; 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