Set oDocName = oWordapp.Documents
You are setting a pointer to the collection of all of the open documents
within the open Word application.
As you may guess, there won't be any.
You are doing this all wrong. Surely you need to go through the
recordset items to get the file name. I would assume that you would need
a variable called 'sFilename' or something.
You aren't even looking in the recordset to get the name of the file out.
What you code is doing is nonsensical; it is doing the following:
- Open an instance of Word.
- Open a recordset showing ALL documents (you are not even doing a WHERE
clause in the SQL either!)
- Go through the recordset one record at a time and not even look at what
is in the record at all! In the meantime check to see if there is a
collection of documents in Word (there will be but it will hold 0
documents) then printout the whole collection of documents.
This is nothing like what you want.
I wrote a message earlier which says the following. May I suggest that
you read it again, work out what I am doing and then compare it with your
message.
===========================
Joanne
If the documents aren't open already then why not open them all from
Access and work on them from there.
To do this you need to create a new instance of Word. You could look for
an existing instance but why trample all over what the user may be doing.
So, you could start off with this instruction somewhere in Access:
dim oWord as Object
set oWord = CreateObject ("Word.Application")
(or whatever variable pointer name you may choose to call it. Helmut
calls it one thing, I tend to put a little 'o' in front of my object
pointers.
Then you get the list of Word documents, let's guess that you do it by
some sort of SQL call and we have an Access RecordSet object, which I will
call oRS. So:
Set oRS = OpenRecordset (........)
if not oRS is Nothing then
do while not oRS.EOF
oRS.MoveNext
loop
end if
This ought to be bog-standard Access code which you are familiar with.
In the middle of this loop you could write, for example:
sFileName = "" & oRS("FileName")
if len(Dir$(sFileName)) > 0 then
Set oDoc = oWord.Documents.Open
if not oDoc is nothing then
oDoc.Printout
oDoc.Saved = True
oDoc.Close
end if
end if
And then, at the end don't forget to close word down with:
oWord.Quit
And that should open one document in your list at a time and then print
it before closing it.
The Len(Dir$()) business is there to make sure that the file exists.
When I check to see if oDoc is not nothing I wanted to make sure that the
document was opened; after all something could have gone wrong.