Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening/closing Word documents with VBA

Status
Not open for further replies.

striker73

MIS
Jun 7, 2001
376
US
I am trying to figure out the VBA syntax to open and close Word documents. This is what I have so far:

Dim wordApp as Word.Application
Set wordApp = GetObject(,"Word.Application")
'If an instance of Word is open, then check to see if FilePath is on of the open documents
With wordApp
For Each Document In wordApp.Documents
If (Document.FullName = FilePath) Then
found=true
Document.Close
End If
Next Document
End With


Essentially, I want to determine if an instance of Word is open, then scroll through the documents, see if the one I'm looking for (FilePath) is open. If it is, then close it.

This document is set to read only, so I'm trying to figure out how to close without saving it.

Also, I get errors saying 'ActiveX component can't create object' when I try to run the code without Word being open. Is there anyway to get around this? Any help would be greatly appreciated!!! Thanks!!!!!
 
Not recalling the word object model myself, I recommend you record a macro in word to close a document without saving... All you have to do is use the fully qualified object including the application object and it will work. Digging in Word's VBA help looking up the objects and properties you get will help... You did install VB help for Word and all your apps, right? For some reason it seems to me that there is an activate method for a document (could be crazy on that one).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top