close word programmatically

J

Joanne

Sorry to have this posted in 2 groups, but I think posting it in the
access group was my error. I believe it is a word issue, not access,
so I re-posted it here.
I have this routine that opens several docs in msword.
The user then views and/or prints the docs one at a time, then closes
each of them. When the viewer closes the last doc, I would like to
close msword and clean up programmatically.

This is the code I am using:
If Not oRst Is Nothing Then
Do While Not oRst.EOF
sfilename = "" & oRst("DocNamePath")
If Len(Dir$(sfilename)) > 0 Then
oWordapp.Visible = True
Set oDocName = oWordapp.Documents.Open(sfilename)
End If
oRst.MoveNext
Loop
End If

oRst.Close
oWordapp.Quit
Set oWordapp = Nothing
Set oDocName = Nothing
Set oRst = Nothing

Problem is it goes thru everything nicely, but closes and cleans up
before the viewer has had a chance to do any work.
I don't know how to program it so that it knows when the last file has
been closed so that it can do its job at that point instead of
immediately.
I tried putting the last 5 lines in an if/end if loop, setting it to
If Len(Dir$(sfilename)) = 0 Then
but that didn't work, so I tried setting the line to read when
If oRst Is Nothing Then
but that also didn't work.

Could you please give me a hand with this, or show me where to read up
on it and help myself?
Thanks
 
H

Helmut Weber

Hi Joanne,
I'd rather handing over control to word entirely,
utilizing an autoclose macro, for example.
And besides that, opening an unknown number of documents,
is no good idea, IMHO. However, if you got control over the
number of documents and their size, here comes one very basic example,
from Excel, by the way, probably costing a lot of performance.
Sub Test444()
Dim oWrd As Word.Application
Set oWrd = New Word.Application
oWrd.Visible = True
oWrd.Documents.Open "c:\test\4.doc"
oWrd.Documents.Open "c:\test\3.doc"
oWrd.Documents.Open "c:\test\2.doc"
oWrd.Documents.Open "c:\test\1.doc"
While oWrd.Documents.Count > 0
DoEvents
Wend
oWrd.Quit
Set oWrd = Nothing
End Sub



Gruss
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Joanne

Helmut
Thank you for your time and consideration of my question

Actually, I have control over the number of files that are being
opened at any one time. I set it up that way, in groups, fearing that
if I let the user open files willynilly, I would have problems with
out of memory or other stuff that crashes everything if they open
everything in sight. So they can only have so many docs open, work
them, print them, whatever, then close them, then close word to return
to the access input form and choose whatever group they might want
next.

I've been thinking that the best way to do the job of closing word
after the last doc is closed would be to do it from word itself, but I
don't know how to run a word macro from my access coding. (I feel
encouraged that I am catching on a bit since you advise that this work
should all be left in the hands of word, and that was my timid
thought!)
If I set up a macro in word to close word and clean up the memory (I
assume that is why we use such things as WdApp = Nothing in access)
how do I call the word macro in my access code? This I realize is an
important thing for a newbie like me to know - not only for this but I
am sure automation will present this need over and over again in a
more full blown app than I am doing here on my first try.

If I had a macro in msword called 'mcrCloseWord', can I call it in an
access routine like this?
oWrd.Macros.Run "mcrCloseWord"?
or maybe DoCmd.Run Macro "mcrCloseWord" - (On this try there is no
reference to msword, so I doubt this will work)

I admit I am shooting in the dark here because I have not been able to
find this type of thing in my books or on the net, but my first try
seems to me to be reasonably like much of the code I've been working
with all along.
Problem is, the tiniest detail, I have learned, can stop the whole
show!!

Anyway, any help you can offer, or points to the info I need will sure
be appreciated.
Joanne
 
H

Helmut Weber

Hi Joanne,
first, I would try what I suggested in the example I gave you.
Seems to work fine here and now. I have asked in the german vb group,
whether doevents costs too much performance. Do not think so,
as doevents is running all the time anyway.
If that fails, I would try Word's autoclose, which has not
to be triggered by access. In autoclose, you could check whether
documents.count = 0
then check whether there is access running (getobject)
and if so, activate access.
But I think,
While oWrd.Documents.Count > 0
DoEvents
Wend
would do the job.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top