我得到一个ms-word的句柄,然后用AccessibleObjectFromEvent得到is的IDispatch(late bingding)。然后我想调用它的属性或方法,它失败了。</p>
但可以使用 c#。
像这样的代码。
function GetProperty(dispobj: IDispatch; PropertyName: widestring;
var retvalue: IDispatch): Boolean;
var
hr: HResult;
DispId1: Integer;
value: Variant;
params: TDispParams;
begin
Result := false;
hr := dispobj.GetIDsOfNames(GUID_NULL, @(PropertyName), 1,
LOCALE_SYSTEM_DEFAULT, @DispId1);
if (hr >= 0) then begin
hr := dispobj.Invoke(DispId1, GUID_NULL, LOCALE_SYSTEM_DEFAULT,
DISPATCH_PROPERTYGET, params, @value, nil, nil);
if (hr >= 0) then begin
retvalue := value;
Result := true;
end;
end;
end;
hWindow := GetWordHandle(Trim(LabeledEdit1.Text));
hWindow := GetChildWndHandle(hWindow, '_WwG');
if `AccessibleObjectFromWindow`(hWindow, 0, IID_IDispatch, WordObject) = S_OK then begin
//GetProperty(WordObject, 'Application', WordApp);
// WordObject.GetTypeInfoCount(nCount);
//Showmessage(IntToStr(nCount));
//WordApplication1.ConnectTo((WordObject.Application) as _Application);
// Showmessage(WordObject.Application.Version)
OleCheck(WordObject.QueryInterface(IID_IDispatch, WordApp));
WordApplication1.ConnectTo(IDispatch(WordApp) as _Application);
Showmessage(WordApplication1.Version)
end