Board index » delphi » Closing Word Document

Closing Word Document

I am having a problem closing a word document from Word.  When word opens I
am doing some formatting of text that I don't want saved to the docuement.
When the user clicks to close the document, I want Word to automatically not
save the changes and close the document.  I have a button on my form that if
the user clicks will accomplish this.

procedure TfrmMain.btnCloseFileClick(Sender: TObject);
var
  SaveChanges: OleVariant;
begin
  SaveChanges := wdDoNotSaveChanges;
  WordApp.Quit(SaveChanges);
  WordApp.Disconnect;
end;

But I would like if this same functionality could be performed when the user
closes the document from within the Word program.  I tried to put this code
in the OnClose event of the TWordDocument but it comes up with a dialog box
that says "Cannot close Word because a dialog is active, Please switch to
Word first and close the dialog"  Any ideas?  Thanks in advance.

Jeff Rasnick

 

Re:Closing Word Document


<<Jeff Rasnick:
I tried to put this code in the OnClose event of the
TWordDocument but it comes up with a dialog box
that says "Cannot close Word because a dialog is active

Quote

procedure TForm1.WordDocumentBeforeClose(Sender: TObject;
var Doc,
  Cancel: OleVariant);
var
  SaveChanges: OleVariant;
begin
  SaveChanges := wdDoNotSaveChanges;
  WordDoc.ActiveWindow.Close(SaveChanges, EmptyParam);
  WordDoc.Disconnect;
  Word.Quit(SaveChanges);
  Word.Disconnect;

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:Closing Word Document


Deborah -
Am I missing something?  My TWordDocument object doesn't show an
OnBeforeClose event in the object inspector.  I'm using Word 97.  Thanks.

Jeff

"Deborah Pate (TeamB)" <d.p...@cableinet.co.not-this-bit.uk> wrote in
message news:VA.00000854.0082802f@cableinet.co.not-this-bit.uk...

Quote
> <<Jeff Rasnick:
> I tried to put this code in the OnClose event of the
> TWordDocument but it comes up with a dialog box
> that says "Cannot close Word because a dialog is active

> procedure TForm1.WordDocumentBeforeClose(Sender: TObject;
> var Doc,
>   Cancel: OleVariant);
> var
>   SaveChanges: OleVariant;
> begin
>   SaveChanges := wdDoNotSaveChanges;
>   WordDoc.ActiveWindow.Close(SaveChanges, EmptyParam);
>   WordDoc.Disconnect;
>   Word.Quit(SaveChanges);
>   Word.Disconnect;

> --
> Deborah Pate (TeamB) http://delphi-jedi.org

>   Use Borland servers; TeamB don't see posts via ISPs
>   http://www.borland.com/newsgroups/genl_faqs.html

Re:Closing Word Document


I wrote too soon, I put that code in the OnClose event and it works as long
as you close the program and not just the window of the document.  I'll
experiment some more, thanks for the help.

Jeff

Quote
"Jeff Rasnick" <rasni...@hotmail.com> wrote in message

news:3b263118$1_1@dnews...
Quote
> Deborah -
> Am I missing something?  My TWordDocument object doesn't show an
> OnBeforeClose event in the object inspector.  I'm using Word 97.  Thanks.

> Jeff

> "Deborah Pate (TeamB)" <d.p...@cableinet.co.not-this-bit.uk> wrote in
> message news:VA.00000854.0082802f@cableinet.co.not-this-bit.uk...
> > <<Jeff Rasnick:
> > I tried to put this code in the OnClose event of the
> > TWordDocument but it comes up with a dialog box
> > that says "Cannot close Word because a dialog is active

> > procedure TForm1.WordDocumentBeforeClose(Sender: TObject;
> > var Doc,
> >   Cancel: OleVariant);
> > var
> >   SaveChanges: OleVariant;
> > begin
> >   SaveChanges := wdDoNotSaveChanges;
> >   WordDoc.ActiveWindow.Close(SaveChanges, EmptyParam);
> >   WordDoc.Disconnect;
> >   Word.Quit(SaveChanges);
> >   Word.Disconnect;

> > --
> > Deborah Pate (TeamB) http://delphi-jedi.org

> >   Use Borland servers; TeamB don't see posts via ISPs
> >   http://www.borland.com/newsgroups/genl_faqs.html

Re:Closing Word Document


<<Jeff Rasnick:
My TWordDocument object doesn't show an
OnBeforeClose event in the object inspector.

Quote

That was a TWordApplication's OnDocumentBeforeClose event.
But Word97 was event poor, it might not have it.

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:Closing Word Document


Deborah-
The code worked to close the document if there is only one document open.
However my application allows the user to open multiple documents.  I tried
to add a for loop in the code to close all the documents, but after the
first document closes the dialog "You cannot close Word because a dialog is
active. Switch to Word first and close the dialog" pops up before closing
any other file.  If you click ok on this dialog then it brings up the save
changes dialog.  Is there a way to close multiple documents?  It seems
strange to me that I have a button on my form that will close all document,
but the same code won't work in the OnClose event.  Thanks for the help! :-)

Jeff

//******************* TfrmMain.DocClose *************************
procedure TfrmMain.DocClose(Sender: TObject);
var
  SaveChanges, ItemNumber: OleVariant;
  currentDoc: Integer;
  docCount: Integer;
begin
  docCount := WordApp.Documents.Count;
  SaveChanges := wdDoNotSaveChanges;
  for currentDoc := 0 to docCount-1 do begin
    WordApp.ActiveDocument.Activate;
    Doc.ActiveWindow.Close(SaveChanges, EmptyParam);
    Doc.Disconnect;
  end;
  WordApp.Quit(SaveChanges);
  WordApp.Disconnect;
end;

"Deborah Pate (TeamB)" <d.p...@cableinet.co.not-this-bit.uk> wrote in
message news:VA.00000858.009c8e24@cableinet.co.not-this-bit.uk...

Quote
> <<Jeff Rasnick:
> My TWordDocument object doesn't show an
> OnBeforeClose event in the object inspector.

> That was a TWordApplication's OnDocumentBeforeClose event.
> But Word97 was event poor, it might not have it.

> --
> Deborah Pate (TeamB) http://delphi-jedi.org

>   Use Borland servers; TeamB don't see posts via ISPs
>   http://www.borland.com/newsgroups/genl_faqs.html

Re:Closing Word Document


<<Jeff Rasnick:
The code worked to close the document if there is only one
document open.

Quote

In general a way to avoid unwanted dialogs is to set the
DisplayAlerts property:
  Word.DisplayAlerts := wdAlertsNone;
  Word.Quit(SaveChanges);

Having said that, I dug out Word97, and I cannot close Word
in the document's OnClose event without causing page
faults. :( Have you had these at all?

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:Closing Word Document


Deborah -
I haven't had any page faults so far, but I still can't get Word to close
without that dialog poping up (if there is more than one document open) even
when I set DisplayAlerts := wdAlertsNone.  Thanks for all the help, let me
know if you figure it out! :-)

Jeff

"Deborah Pate (TeamB)" <d.p...@cableinet.co.not-this-bit.uk> wrote in
message news:VA.00000863.00015ef4@cableinet.co.not-this-bit.uk...

Quote
> <<Jeff Rasnick:
> The code worked to close the document if there is only one
> document open.

> In general a way to avoid unwanted dialogs is to set the
> DisplayAlerts property:
>   Word.DisplayAlerts := wdAlertsNone;
>   Word.Quit(SaveChanges);

> Having said that, I dug out Word97, and I cannot close Word
> in the document's OnClose event without causing page
> faults. :( Have you had these at all?

> --
> Deborah Pate (TeamB) http://delphi-jedi.org

>   Use Borland servers; TeamB don't see posts via ISPs
>   http://www.borland.com/newsgroups/genl_faqs.html

Other Threads