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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close Word Document

Status
Not open for further replies.

shannanl

IS-IT--Management
Apr 24, 2003
1,071
US
I am using the following to open up a word document so the user can type information into it. When they are done, they save the document and then exit. Winword.exe is still running after the first document and as such causes the program to crash when it tries the 2nd time. How can I completely close the document and the word application?

'-- START UP WORD
Dim wdApp As Word.Application
Dim wDoc As Word.Document

'-- TEMPLATE NAME
Dim strDocName As String
strDocName = "c:\transcriptions\template3"

Set wdApp = New Word.Application
Set wDoc = wdApp.Documents.Open(strDocName)

'-- SHOW WORD DOC
wdApp.Visible = True

Thanks,

Shannan
 
Hi,

I do it that way :

--------------------------------------------------
wDoc.Close wdSaveChanges '(Or wdDoNotSaveChanges)
wdApp.Quit
Set wdApp = Nothing 'free memory
--------------------------------------------------
Lemox
 
Lemox,

Where would you put this? Is there an "unload" event for a word doc, like the one for a form?

Thanks,

Shannan
 
1. Your code:
Code:
Dim strDocName As String
    strDocName = "c:\transcriptions\template3"
    
    Set wdApp = New Word.Application
    Set wDoc = wdApp.Documents.Open(strDocName)

The file you are opening appears to be the template file itself. If this is the case, this is a bad practice.

2. If you want to invoke a template file, that is, to make a clone of it (but leave the template file itself intact - and this is what really should be done with template files - then use:

Code:
Set wdApp = New Word.Application

wdApp.Documents.Add Template:= _
        "[i]full path[/i].dot", NewTemplate:=False, DocumentType:=0

This creates a new document BASED on the template, not open the template file itself.

3. You are using Set to make an explicit object of the original file. When the user does a save (and I am assuming a SaveAs) then you MUST use:

Set wDoc = Nothing

This releases (destroys) the object - which is why it was persisting. You had not destroyed it.

4. As Lemox noted, you should also use
Set wdApp = Nothing

as well, to release that object.

Gerry
 
Oh sorry... misunderstood.

Well : strange that your Word app still exists after the user has shut it down.
I don't use it that way but I just tried it with your piece of code : when I exit Word, the WinWord.exe process really disappears from my task list..

... and yes it is possible to know when the Word Application is killed
but it's quite 'intricate'. And it seems you want the user to exit Word by himself so the 3 lines I gave would probably crash... b coz the doc and the app would (normally) no longer exist !


Are you sure there is no other word process created at another place in your code ?

 
Gerry,

Thanks for the info. I will change the template, that is a good suggestion. I am still somewhat confused on where to place the set wdoc = nothing code. How do I use the = nothing when the user saves the doc? Is there somewhere within the template itself that I place the code?

Shannan
 
OK, walk me through this.

Are you invoking a real template, that is a file with the .dot extension? Invoking a template (NOT opening the file) creates a clone of that template file.

Or are you opening a .doc file that the user does whatever it is they are doing, then saves the file AS ANOTHER file?

This point is critical. Can't really move forward until that is clear.

Plus, where are doing this from? I notice you are creating an instance of Word. I assume this means you are doing this from Excel, or Access or something else VBA compliant. But not from Wprd. Please state what app you are doing this from. It is not totally important, I am curious, is all.

How are you collecting the information from the user?
How are you getting the information intothe document?
Do you want the new document, with the user information, to close automatically, as in saved automatically? Or do you want user choice about that?

Gerry
 
Ok. Right now I am opening a document file. I will create a template but now its just a document. The user opens the file and I assign a file name to the file and save it to a certain location. The user manipulates info on the document they opened and clicks the save button. The document is saved to the location with the filename I gave it (actually the 2nd time it is saved - first is when they open it up). The user closes it and goes to the next one. This is from a VB application.

This application is a medical records application. The user will select procedures from a list, click on a certain procedure. This will open the Word template with certain information filled in already (patients name, medical record number, date of exam, etc.) The medical record transcriptionist will then type in notes about the procedure and save it. I get the file name from reading the procedure key from a database. The information I am inserting into the doc (name, account, etc.) is coming from functions to a SQL server db. The user will manipulate the data on the word doc and then close it. Its at that point that I need to close Word (I guess).
Its working quite well except for that fact it is hanging on to Word and not closing it.

Shannan
 
OK, using a document todo this is fine. Explicitly saving it with SaveAs to a location before presenting to the user is a GOOD idea.

'-- START UP WORD
Dim wdApp As Word.Application
Dim wDoc As Word.Document

'-- TEMPLATE NAME
Dim strDocName As String
strDocName = "c:\transcriptions\template3"

Set wdApp = New Word.Application
Set wDoc = wdApp.Documents.Open(strDocName)

NOTE: it sounds like you are making adjustments to this file, based on criteria.

This will open the Word template with certain information filled in already
THEREFORE: there must be processing of the document, based what you need to fill in. Yes?

'--Save file to specified location
wDoc.SaveAs Filename:=blahblah.doc

'-- NOW show Word doc
wdApp.Visible = True

'-- User does their thing
blah blah blah
'-- and saves

' now YOU close it explicitly
' and destroy the object, as the current object
' is now the changed file
wDoc.Close (wdDoNotSaveChanges)
set wDoc = Nothing
'--open the original file again for the next user interaction

Set wDoc = wdApp.Documents.Open(strDocName)

' do what ever it is you are filling in for the next file
' AND do you file saveas thing again

' NEXT file for user to do whatever

'-- at the LAST file

Set wDoc = Nothing
Set wdApp = Nothing.


This keeps the instance of Word alive, while you are processing however number of files that need to be done.

It is also possible to destroy the Word.Application each time, after each file; and then recreate it again for each file. i am not sure thatwould be a good idea.

If you know the number of files to be processed, simply loop through the above steps.

How are you filling the data, the data before the user sees it?

At the end of your last post you say you want to close Word after the user manipulates the data on the document. Please confirm that, because you also mention a "next". You have to decide if you want to close Word each time, or not.

In my opinion, this calls for a nicely designed UserForm. It could have fields with prefilled in data (from your SQL db), fields the user completes; they press OK, the document is created and saved; the userForm is refreshed with the data from the next file that needs manipulating.

How are you getting an idea of how many files need to be manipulated? Never mind...does not matter. What matters is if you know how many. if you do, then again, loop through the process for as many times as you need.

UserForm would be better, but you probably do not have the development time.....


Gerry
 
Gerry,

I am replacing an old application that does this and I want to keep mine as close to the old one in functionality as I can so as not to confuse the users. I am populating all the info on the form using functions within vb and then using labels on the word doc and populating them that way. I have no idea of knowing how many docs the user will be working with. They will creating and modifying word docs throughout the day.

Shannan
 
OK, but when the user starts this process, do they say to the originating VB app - "I have THIS many to work with". In which case, you have to have some sort of parameter being passed. If may be 1, it may be 5, it may be 25. But it is a number.

How else are you filling in data for each individual file?

Or do they load the app one at a time?

If it is one at a time, then yes, destroy the Word application object each time.

And bless you for putting the data on the forms as labels. I see far too many forms where people think "Text?"...needs a textbox then. Great. So the user sees the form for that particular file. They fill in what they need to fill in. Press OK (I assume), and whooooosh, your filled in data, plus the user filled in data, is inserted into the file.

1. why don't you just save the file immediately when they click OK on the form? why do you wait for the user to manually click the save button?

2. if there is a next file to be processed, how are you loading that, at the present?

Gerry
 
OK, a patient comes into the hospital and has, lets say a CT scan of the brain. A few days later the doctor, who uses a dictation machine to dictate his findings is finished with this reading. The transcriptionist has a machine that has lots of the transcriptions on it, from many different doctors and cases. They play each one in sequence. When the trasncription starts, the doctors says something like "Patient: John Doe, 9/20/2004, CT Brain". The transcriptionist stops the tape and brings up that patient and procedure, clicks on it and the word doc is opened with the necessary information about the patient and procedure already on it. The transcriptionist then types in the dictation from the doctor, adds a signature line using a macro, depending on which doctor it is and prints off the document for signature of the doctor. They have no way of knowing how many they are to do when they start. They just have to do them one at a time.

I actually save the word document when the user first clicks on the procedure they want to work with. I assign it a unique number that keys into a table in by db and I save the doc in the correct location depending on what type of reading it is, we have 6 different ones that gets saved into a different folder. That way the document is saved should something happen. The user then adds the necessary dictation info and saves the doc as a completed document.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top