TOleContainer打开.tmp(word2016)文件Delphi7

TOleContainer opening .tmp(word2016) documents Delphi7

我们正在使用 Delphi7,SQL 服务器 2008 和 MS word 2003。现在我们计划将 MS word 2003 迁移到 MS word 2016。

在我们的应用程序中,我们将从数据库加载文档(BLOB 字段)并将其保存到 .tmp 文件中,然后我们将在 Delphi7.

中使用 TOleContainer 打开文档

以上过程适用于 .doc,不适用于 .docx。我们可以用下面的例子重现这个问题

  1. 创建 .docx 文件
  2. 打开 .docx 文件并输入一些文本并保存并关闭
  3. 将 .docx 重命名为 .tmp
  4. 使用 TOleContainer 打开 .tmp 文件(Delphi 代码下方)

Delphi代码:

procedure TForm1.FormShow(Sender: TObject);
begin
  WordOleContainer.AllowInPlace := True;
  WordOleContainer.CreateObjectFromFile('F:\WordViewerTest\docx.tmp', False);
end;

当我们 运行 我们在下面 error.if 我们点击打开文档的应用程序正在另一个 window 中打开,而不是在 olecontainer 中。

如果我双击 .tmp 文件,它会在没有任何警告的情况下打开。 如果我使用上面的 delphi 代码,它会显示警告。如何使上面的代码工作?

只要 *.docx 文件从二进制的角度来看实际上是 *.zip 文件,就无法区分 OLE 二进制模式 class 搜索机制(在 GetClassFile reference, step 2. of determining strategy) between those file types. If you registered a binary pattern for *.docx file, you would actually register *.zip files for opening by an application of your choice. Since then all the programs using OleCreateFromFile 将在您注册的应用程序中打开 *.zip 文件。

因此,要在系统中毫无歧义地解决您的问题,只需使用正确的 *.docx 扩展名保存文件,然后让 OLE 通过文件扩展名找到 class(确定策略的第 3 步)。