jackjohnson44
asked on
visual basic close word document
I have a vb app that writes to a word file.
Sometimes, I want to close the file, and pretty much throw it away.
When I go to my close file code, and quit the application, I am prompted by word to save my changes.
My question is, how do I close a word document (made from a document template) in a way that is transparent to the user.
here is some of my code
'this is how I open and write
Set oWord = CreateObject("Word.Applica tion")
With oWord
.Documents.add (App.path & "\RptStandard.dot")
.ActiveDocument.Bookmarks( "modelNum" ).Select
.Selection.TypeText "ASDFASDF"
'here is how I close the document, but the first line causes word to prompt me for a save
oWord.ActiveDocument.Close
oWord.Quit
Set oWord = Nothing
Sometimes, I want to close the file, and pretty much throw it away.
When I go to my close file code, and quit the application, I am prompted by word to save my changes.
My question is, how do I close a word document (made from a document template) in a way that is transparent to the user.
here is some of my code
'this is how I open and write
Set oWord = CreateObject("Word.Applica
With oWord
.Documents.add (App.path & "\RptStandard.dot")
.ActiveDocument.Bookmarks(
.Selection.TypeText "ASDFASDF"
'here is how I close the document, but the first line causes word to prompt me for a save
oWord.ActiveDocument.Close
oWord.Quit
Set oWord = Nothing
Hi, try this:
oWord.ActiveDocument.Close False
---
Harish
oWord.ActiveDocument.Close
---
Harish
jackjohnson44, and if you want to save the file, then use this syntax:
oWord.ActiveDocument.Close True, "filename.doc"
oWord.ActiveDocument.Close
ASKER
Is there a way to tell if the object was actually created?
If I have an error before I make it, with he line
Set oWord = CreateObject("Word.Applica tion")
I want to be able to see if it is open, so I can close it.
If I have an error before I make it, with he line
Set oWord = CreateObject("Word.Applica
I want to be able to see if it is open, so I can close it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, this line gives me an error, what does it do?
Set oWord = Get(,"Word.Application")
Set oWord = Get(,"Word.Application")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I see that some answers refer to ActiveDocument. It would be better to stick to using your oDoc object. ActiveDocument will usually still get the same document, but should you ever have more than one document open, you wouldn't have to keep track of which one is active.
anyhow, you should use a variable for the document, and avoid the Selection object as much as possible.
Set oWord = CreateObject("Word.Applica
With oWord
set oDoc = .Documents.add (App.path & "\RptStandard.dot")
end with
with oDoc.Bookmarks("modelNum")
Range.Text = "ASDFASDF"
end with
etc.
oDoc.Close save:=false
set oDoc = nothing
oWord.Quit
set oWord = nothing