无法在 Word 应用程序的 onquit 事件上加载到流

发布于 2024-09-15 10:19:05 字数 729 浏览 0 评论 0原文

我打开一个文档

WordApplication1.Connect;
WordApplication1.Documents.OpenOld(FileNameOLE,EmptyParam,EmptyParam,EmptyParam,
    EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
WordApplication1.Visible:=true;

,并想在关闭后将其保存到流中,但出现以下错误。

"Cannot open file used by another process." 

该怎么办 ?感谢您的帮助。

样本

procedure TPatient.WordApplication1Quit(Sender: TObject);
  var mem:TMemoryStream;
begin
  WordApplication1.Disconnect;
  WordApplication1.Quit;
  //I get filename to global widestring variable File_OLE
  mem:=Tmemorystream.Create;
  mem.LoadFromFile(File_OLE); -------->>>>error here
  mem.Position:=0;

I open a document with

WordApplication1.Connect;
WordApplication1.Documents.OpenOld(FileNameOLE,EmptyParam,EmptyParam,EmptyParam,
    EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
WordApplication1.Visible:=true;

and I want to save it to a Stream after it is closed but I get below error.

"Cannot open file used by another process." 

What to do ? Thanks for help.

Sample

procedure TPatient.WordApplication1Quit(Sender: TObject);
  var mem:TMemoryStream;
begin
  WordApplication1.Disconnect;
  WordApplication1.Quit;
  //I get filename to global widestring variable File_OLE
  mem:=Tmemorystream.Create;
  mem.LoadFromFile(File_OLE); -------->>>>error here
  mem.Position:=0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-09-22 10:19:05

Word 似乎仍然阻止对该文件的访问。所以你可能需要等待几毫秒到几秒直到文件被释放。您可以通过睡眠和/或重试保存几次来实现此目的

It seems like Word is still blocking the access to the file. So you may have to wait a few milliseconds to seconds until the file is released. You can achieve this by Sleep and/or retrying to save a few times

︶ ̄淡然 2024-09-22 10:19:05

在示例过程中,当您想要将文档加载到 TMemoryStream 中时,Word 仍在运行。 Word当时正在处理它的退出事件(见图:P)并且尚未关闭。

您可以做的是在退出事件上启动计时器(TTimer,间隔为 500/1000),并在计时器事件中打开文档。另外,不要调用 WordApplication1.Quit,因为您已经退出,只需断开 WordApplication1 的连接即可。

更好的解决方案是不依赖 quit 事件。因为当您像第一个代码部分中那样启动 Word 并从 Windows 资源管理器打开另一个 Word 文档时,当您关闭应用程序打开的文档时,退出事件不会发生。 Word 仍将保持活动状态,因为它也托管其他文档。

将 TWordDocument 添加到表单中,然后在打开文档时将 TWordDocument 连接到打开的文档。在 TWordDocument.Close 事件中,您可以启动计时器,当计时器触发时,您可以加载文档。更好的方法是不使用计时器,而只是将 OnClose 事件上的文档保存到另一个文件 (.SaveAs) 并将该文件加载到内存流中。这样您就可以确定该文件不是由Word 打开的。

In your sample procedure, Word is still running when you want to load the document in your TMemoryStream. Word is handling it's quit event at that time (go figure :P ) and hasn't shutdown yet.

What you could do is start a timer (TTimer with an interval of 500/1000) on the quit event and open your document in the timer event. Also don't call WordApplication1.Quit, because you are already quitting, just disconnect your WordApplication1.

An even better solution is to not rely on the quit event. Because when you start Word as in your first code section, and you open another Word document from Windows Explorer, the quit event won't come by when you close the document your application opened. Word will still remain active because it's hosting the other document too.

Add a TWordDocument to your form and when you open the document, connect the TWordDocument to the opened document. In the TWordDocument.Close event you can start a Timer and when the Timer fires, you can load your Document. An even better way is to not use timers but just save the document on the OnClose event to another file (.SaveAs) and load that file into the memorystream. This way you can be sure that the file is not opened by Word.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文