Link to home
Start Free TrialLog in
Avatar of jackjohnson44
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.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
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

 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      
Hi, try this:

 oWord.ActiveDocument.Close False

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

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

Avatar of jackjohnson44
jackjohnson44

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

I want to be able to see if it is open, so I can close it.
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, this line gives me an error, what does it do?

Set oWord = Get(,"Word.Application")
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.