从程序资源中读取 Word 文件 (*.dot)

Reading Word-file (*.dot) from program resources

有一组 Word 模板(文件 *.dot)和一个小程序,可以根据该模板创建新文件。它工作正常,但目标是将所有内容都放在一个 exe 文件中。 我看到解决方案是将模板文件移动到程序资源中。但我不知道,我将如何从资源中读取它们。请告诉我该怎么做。 也许你可以告诉我另一个解决方案。

现在,我的代码是:

procedure TfmMain.CreateDocument0;
var
  TempleateFileName: string;
  WordApp, Document: OleVariant;

  procedure FillBookmark(BookmarkName, bText: string);
  var
    Range: OleVariant;
  begin
    if Document.Bookmarks.Exists(BookmarkName) then
    begin
      Range := Document.Bookmarks.Item(BookmarkName).Range;
      Range.Text := bText;
    end;
  end;
begin
  TempleateFileName := ExtractFilePath(Application.ExeName)+'Templates[=10=].dot';
  try
    WordApp := GetActiveOleObject('Word.Application');
  except
    try
      WordApp := CreateOleObject('Word.Application');
    except
      on E: Exception do
      begin
        MessageBox(Self.Handle, PChar(E.Message), PChar(fmMain.Caption), MB_OK+MB_ICONERROR);
        Exit;
      end;
    end;
  end;

  try
    Document := WordApp.Documents.Add(TempleateFileName, False);

    FillBookmark('ObjectType', edt0ObjectType.Text);
    ...

    WordApp.Visible := True;
    WordApp.Activate;
  finally
    WordApp := Unassigned;
  end;
end;

也就是说,我应该改变这一行: Document := WordApp.Documents.Add(TempleateFileName, False);

不是从文件中读取,而是从程序资源中读取。

Word 无法从内存中打开文档。它不仅没有这样的功能,你还必须记住,Word 是在一个单独的进程中执行的。它看不到您进程中的内存,即使它能够从内存中打开文档。

如果您确实将文档放入链接资源中,则需要先将它们提取到文件中,然后再让 Word 打开它们。