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.Application")
   
    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
jackjohnson44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
 oWord.ActiveDocument.Close save:=false
 

anyhow, you should use a variable for the document, and avoid the Selection object as much as possible.

   Set oWord = CreateObject("Word.Application")    
    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      
0
Harisha M GEngineerCommented:
Hi, try this:

 oWord.ActiveDocument.Close False

---
Harish
0
Harisha M GEngineerCommented:
jackjohnson44, and if you want to save the file, then use this syntax:

 oWord.ActiveDocument.Close True, "filename.doc"

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jackjohnson44Author Commented:
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.Application")

I want to be able to see if it is open, so I can close it.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:

CloseWord:
on error resume next
set oWord = nothing
Set oWord = Get(,"Word.Application")
if not(oWord is nothing) then
  'word was already open, close it
  oWord.Quit
  Goto CloseWord 'go and close other word instances :-)
end if

0
jackjohnson44Author Commented:
Thanks, this line gives me an error, what does it do?

Set oWord = Get(,"Word.Application")
0
Harisha M GEngineerCommented:
Try this:

If(Not oWord Is Nothing) Then
   ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, should be GetObject:
Set oWord = GetObject(,"Word.Application")
0
GrahamSkanRetiredCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.