ObjGet
检索程序或文件名引用的 COM 对象.
ObjGet ( "filename" [, "classname" [, instance]] )
参数
filename
|
对象文件的完整路径和名称(见备注).
|
classname
|
[可选] 类标识符. 可以是 ProgID 或 CLSID 的字符串表示形式.
|
instance
|
[可选] 对象的实例, 相同于 (CO) 类 ROT 对象.
|
返回值
成功:
|
返回一个对象.
|
失败:
|
@error 设置 为非 0 值.
|
备注
如果只使用类标识符, 则文件名为可选, 但不能省略参数.
如果你想运行对象表(ROT)访问对象, 可以使用空字符串. 此时可以使用第三个参数指定访问的实例.
所有其它情况下, 第三个参数将被忽略.
如果您使用一个文件名, 类名是可选的. 仅当你想加载一个特定的类对象时使用类名.
类名参数没有默认值. 如果它被指定, 则它将会处理.
有关对象的更多信息参见 Obj/COM 参考.
相关
GUICtrlCreateObj, IsObj, ObjCreate, ObjEvent, ObjName
函数示例
示例 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
示例 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
----------------------------------------
|