abbas abedi -mahmood
abbas abedi -mahmood

Reputation: 23

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.

 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

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

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

Upvotes: 2

Views: 4676

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

You could write your own event sink to listen to the Word application's OnQuit event. However, it's going to be easier to switch to early bound COM. The import type library, found in Word2000.pas, contains all that you need.

  • Use TWordApplication for your application object.
  • 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.

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;

Upvotes: 6

Related Questions