在通过OLE的Word自动化中,当访问ActiveDocument属性时,如果当前没有可见文档可用,则会引发异常(至少在Delphi中),因此,我的目标是像IsActiveDocumentValid那样进行一些测试,如何在没有举例外?谢谢!
答案 0 :(得分:1)
自动化服务器本身引发异常,您无法阻止它。但是,在访问ActiveDocument
;
Documents
的计数
WordApplication.Documents.Count
如果没有可用的文件,如果'Count'为0。
修改强> 或者,您可以静默处理特定的异常,例如(Delphi代码);
function ActiveDocumentExists(WordApplication: Variant): Boolean;
begin
Result := True;
try
WordApplication.ActiveDocument.Activate;
except on E: EOleException do
if E.ErrorCode = LRESULT($800A1098) then
Result := False;
end;
end;