[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

visual basic close word document

Posted on 2006-04-18
9
Medium Priority
?
497 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:jackjohnson44
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16481101
 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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16481103
Hi, try this:

 oWord.ActiveDocument.Close False

---
Harish
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16481123
jackjohnson44, and if you want to save the file, then use this syntax:

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

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:jackjohnson44
ID: 16481272
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
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1200 total points
ID: 16481359

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
 

Author Comment

by:jackjohnson44
ID: 16481391
Thanks, this line gives me an error, what does it do?

Set oWord = Get(,"Word.Application")
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 800 total points
ID: 16481428
Try this:

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

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1200 total points
ID: 16481472
sorry, should be GetObject:
Set oWord = GetObject(,"Word.Application")
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16481499
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question