以下代码打开“app”参数指定的文档,然后等待特定文档关闭。这适用于所有文档类型,除非您打开一个 Excel 工作簿并打开另一个 Excel 工作簿。代码认为文档已关闭,但实际上文档仍处于打开状态。我该如何解决这个问题?
procedure RunAppAndWAit( a: TApplication; app, par, verb: string);
var
seinfo: tshellexecuteinfo;
exitcode: dword;
begin
fillchar( seinfo, sizeof( seinfo), 0);
seinfo.cbsize := sizeof( tshellexecuteinfo);
with seinfo do
begin
fmask := see_mask_nocloseprocess;
wnd := a.Handle;
lpfile := pchar( app);
lpDirectory := pchar( ExtractFileDir( app));
lpParameters := pchar( par);
lpVerb := pchar( verb);
nshow := sw_shownormal;
end;
if ShellExecuteEx( @seinfo) then
begin
repeat
a.ProcessMessages;
GetExitCodeProcess( seinfo.hprocess, exitcode);
until ( exitcode <> still_active) or a.terminated;
end
else
sshowmessage( 'Unable to open ' + app);
end;
最佳答案
您的尝试仅适用于在启动文档的同一进程中打开文档的应用程序。
许多应用程序不再以这种方式工作:启动文档的进程会将文档传递给另一个显示/编辑它的进程,然后启动进程就会终止。
您需要找到一个支持事件回调的 API(在本例中为 Excel,很可能是 Excel 公开的 COM API),以便您更仔细地观察 Excel 对文档的实际操作。
使用此 API 打开文档,注册一个在文档关闭时调用的事件,等待该事件,然后关闭。
关于delphi - 使用 Delphi 确定 Excel 工作簿何时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4233338/