无法在 Word 应用程序的 onquit 事件上加载到流
我打开一个文档
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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在示例过程中,当您想要将文档加载到 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.