Closing Word Document

G

Guest

I want to close a Word document and tried this code:


Set wordApp = GetObject(, "Word.Application")
Set WordDoc = wordApp.Documents.Open(file_name)
With wordApp

.Documents.Close

End With

How can I check wether the document is open? And how can I close only
the document, not all open word documents?
 
R

RoyVidar

jokobe wrote in message
I want to close a Word document and tried this code:


Set wordApp = GetObject(, "Word.Application")
Set WordDoc = wordApp.Documents.Open(file_name)
With wordApp

.Documents.Close

End With

How can I check wether the document is open? And how can I close only
the document, not all open word documents?

In stead of your with block, try

WordDoc.Close

To check whether a document (file) is open, you could probably loop the
documents collection.

for each oDoc in wordApp.Documents
if oDoc.Name = <name of document> then
' document is open
exit for
end if
next oDoc
 

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