Close word window usering VBA

K

Kezza

Hi There
I am trying to work out how to close a single word window without closing
any other word windows that might be open.

The issue is that if this is the only document left open.. it closes the
document but still leaves the word window open.

I have tried the following
Me.Application.ActiveWindow.Close
Windows("conquest report.doc").Parent.Close
SaveChanges:=wdDoNotSaveChanges
ActiveDocument.Close wdDoNotSaveChanges
Windows("A.doc").Application.Quit

Non of this code gives me the solution I am looking for.

I would really appreciate some assistance.

Thanks
 
V

Valmont

Hello Kezza.

Here's an idea which you can use often:

1) Open a few documents
2) Start the Macro Recorder
3) Close a single document (leave the others open)
4) Stop the Macro Recorder
5) Press "F11" to go into the IDE and learn from that code
6) Type a few things of this code in google, and find out how others use
this code. They may have smarter or better ideas which handle odds and ends.

Basically this is your code:

Windows("Doc2.doc").Activate
ActiveDocument.Close

So your combinations were close, but you haven't tried this one yet :)

~Val
 
K

Kezza

Hi There
thanks,but this does not work for me. I did try that combination and it
does not work.

If the word document is the only document open and you close the document
(or active window) it leaves the word window open. I would like a command
that closes this window as well.

Thanks
 
J

JP

You would need to set an object reference to the Word Application
object, then call the .Quit method to close that instance. For
example:

Dim objWdApp As Word.Application

Set objWdApp = New Word.Application

objWdApp.Quit



HTH,
JP
 
K

Kezza

Wont the following line of code do the same thing.
Me.Application.Quit

Also I would like to close the instance I am in... not call it from another
instance.
 
K

KeithM

This works for me in Word 2003

ActiveDocument.Close
If Documents.Count = 0 Then
Application.Quit
End if

Hope that helps!
 
K

Kezza

Thanks Keith
Thats excellent... don't know why I didn't think of it. I am using the 2002
version of word so the code for me was

If Documents.Count = 1 Then
' Close word without saving
Application.Quit wdDoNotSaveChanges
Else
' Close document without saving
ActiveDocument.Close wdDoNotSaveChanges
End If
Thanks very much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top