简体   繁体   English

在delphi中添加新的word文档

[英]add new word document in delphi

I am buliding a delphi form to add a new word document in MS Word and wait for the user to insert text and edit document, save the file and exit form MS Word, then get me the file name and path to the file. 我正在创建一个delphi表单,以便在MS Word中添加新的Word文档,并等待用户插入文本并编辑文档,保存文件并退出MS Word表单,然后为我获取文件名和文件路径。

 WordApp := CreateOleObject('Word.Application');
 WordApp.Visible := True;
 Doc := WordApp.Documents.add();

wait for user insert text and edit document and save file and exit form MS Word THEN 等待用户插入文本并编辑文档并保存文件并退出表格MS Word THEN

Doc.Save;
DocName := Doc.Name;
Docpath := IncludeTrailingPathDelimiter(Doc.path) + DocName;

with ZipForge1 do
begin
  FileName := Zipfilename;
  OpenArchive;
  Options.StorePath := spNoPath;
  AddFiles(Docpath);
  CloseArchive;
end;

You could write your own event sink to listen to the Word application's OnQuit event. 您可以编写自己的事件接收器来侦听Word应用程序的OnQuit事件。 However, it's going to be easier to switch to early bound COM. 但是,切换到早期绑定的COM将更加容易。 The import type library, found in Word2000.pas , contains all that you need. Word2000.pas找到导入类型库,其中包含您需要的所有内容。

  • Use TWordApplication for your application object. TWordApplication用于您的应用程序对象。
  • Assign your handler(s) to the events of the application object. 将您的处理程序分配给应用程序对象的事件。
  • Depending on precisely what you want to do, the events that seem most applicable are OnDocumentBeforeClose and OnQuit . 取决于您要执行的操作,似乎最适用的事件是OnDocumentBeforeCloseOnQuit

To illustrate, here's the most trivial example that I can devise: 为了说明这一点,这是我可以设计的最简单的示例:

uses
  Word2000;

procedure TForm1.Button1Click(Sender: TObject);
var
  WordApp: TWordApplication;
begin
  WordApp := TWordApplication.Create(Self);
  WordApp.Visible := True;
  WordApp.Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  WordApp.OnQuit := WordAppQuit;
  WordApp.OnDocumentBeforeClose := WordDocumentBeforeClose;
end;

procedure TForm1.WordAppQuit(Sender: TObject);
begin
  ShowMessage('Word application quit');
end;

procedure TForm1.WordDocumentBeforeClose(ASender: TObject; 
  const Doc: WordDocument; var Cancel: WordBool);
begin
  ShowMessage(Doc.Name + ' closed');
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM