I am using the Word2000.pas automation server components with Delphi 5, and things are going fine except that when I set Visible for the Word application, it affects all open Word documents, including those that were opened by the user. Also Close displays the same behavior, of closing the user's open Word documents in addition to the ones I opened programmatically.
It seems that I am connecting to an existing instance of Word rather than starting a new instance. How do I keep my Delphi-automated Word application and documents from affecting the user's existing open Word app windows? I am new to automating Word, and am probably missing something very basic about how to connect to Word and leave the user's existing open windows alone.
{ Create and print letters based on MS-Word }
Unit uWordLetters;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Word2000, OleServer ;
type TMSWordServer = class(TObject) WordApplication: TWordApplication; Doc: TWordDocument; ParagraphFormat: TWordParagraphFormat; WordFont: TWordFont; constructor Create(ParentComponent: TComponent); procedure CreateNewDocument(ParentComponent: TComponent); procedure Close; procedure EnterText(InputText: string); procedure Print; private public { Public declarations } end;
implementation
uses ActiveX, Clipbrd;
{Create an instance of MS-Word, and an empty document} constructor TMSWordServer.Create(ParentComponent: TComponent); begin { In this example, the server's Autoconnect property is false, so we start Word in code } inherited Create; WordApplication := TWordApplication.Create(ParentComponent); ParagraphFormat := TWordParagraphFormat.Create(ParentComponent); WordFont := TWordFont.Create(ParentComponent); WordApplication.Connect; WordApplication.Visible := True; // WordApplication.Visible := False; << This also hides the user's open Word doc windows end;
procedure TMSWordServer.CreateNewDocument(ParentComponent: TComponent); begin Doc := TWordDocument.Create(ParentComponent); Doc.ConnectTo(WordApplication.Documents.Add(EmptyParam, EmptyParam, EmptyParam,EmptyParam)); Doc.Bookmarks.Add('StartOfDocument', EmptyParam); end;
{Close Word and disconnect; do not save open document(s)} procedure TMSWordServer.Close; var SaveChanges: OleVariant; begin { Make sure we aren't prompted to save the document } if Assigned(WordApplication) then begin SaveChanges := wdDoNotSaveChanges; WordApplication.Quit(SaveChanges); WordApplication.Disconnect; end; end; end.