Print a list of Word documents without opening them

S

Sally Green

Hi

Access 2003 and Word 2003 files. Is it possible to print a list of Word
documents from within an Access form without having to open each document in
Word and print it?

I have a subform which lists all the WordDocs that relate to the primary key
field in the Main form. (The Main form/Sub form link works fine). The
SubForm has a field WordDocs which is a
Hyperlink data type. So each record hyperlinks to 1 Word document. I have
put a button in the Main form to print each of the related Word documents
but don't know how to code this button.
So if there are 8 Word documents I would like to be able to click this button
and have all eight word documents print without having to open each, print,
close etc.

Could you please let me know if this is possible and give me an example of
code. Assume Main form is called frmMF and sub form is called frmSF and
field is called WordDocs. ie form!frmMF.form!frmSF!WordDocs

The Word documents don't have to be hyperlinks if it's possible to do this
as text datatype instead.

Many thanks in advance.
 
A

Albert D. Kallal

I would approach this like:

Dim rst As DAO.Recordset
Dim wordapp As Object
Dim WordDoc As Object ' one instance of a word doc

Set wordapp = CreateObject("Word.Application")

Set rst = Me.SubFormContorl.Form.ReocrdSetClone
rst.MoveFirst
Do While rst.EOF = False
Set WordDoc = wordapp.Documents.Open(rst!WordDocName)
WordDoc.PrintOut
WordDoc.Close (False)
rst.MoveNext
Loop

Set wordapp = Nothing
Set WordDoc = Nothing


The above is "air" code...but, I looks quite close to what you need....
 
S

Sally Green

Thanks for your prompt reply. I've tried a variety of things around this but
can't get it to work. It hangs up on Me.SubFormControl - msg reads: Method
or data member not found.

My field name is WordDoc so I've changed your Object to be WordDocToPrint so
there's no confusion and this is what I've put:

Private Sub cmdPrintWordDocs_Click()
'On Error GoTo EH

Dim rst As DAO.Recordset
Dim WordApp As Object
Dim WordDocToPrint As Object

Set WordApp = CreateObject("Word.Application")

Set rst = Me.SubFormControl.Form.RecordsetClone

rst.MoveFirst
Do While rst.EOF = False
Set WordDocToPrint = WordApp.Documents.Open(rst!WordDoc)
WordDocToPrint.PrintOut
WordDocToPrint.Close (False)

rst.MoveNext
Loop

Set WordApp = Nothing
Set WordDocToPrint = Nothing

Exit Sub

'EH:
'MsgBox Err.Number & ": " & Err.Description
End Sub

One of the variations i use was to Dim the database and set the recordset to
the underlying table. I then put an if statement in to check the value of
the link child field in the subform. This worked ok until the line:

Set WordDocToPrint = WordApp.Documents.Open(rst!WordDoc)

It wouldn't recognise the hylinked field. So I added an additional field to
store the full path and name of the doucment but that wouldn't work either.

ONE THING I FORGOT TO MENTION IS THAT THERE ARE 4 SUBFORMS IN THE MAIN FORM.

Any other ideas would be appreciated. Thanks.
 
A

Albert D. Kallal

SubFormControl

I assume you going to change the name of this control to that of what the
actual name of the sub form control you used (note that this name is not
always the same as the underlying sub-form name, we are talking about the
*control* name here that is a sub-form control.

I would be a *great* developer I could read your mind..and actually "know"
the name of the sub-form control on your form...

Your assumption to change the name of the field in your sub-form was a
correct, and good assumption.

Note that the document name (rst!WordDoc) must also include the full
qualified path name to the document for word to correctly open the
document....
 
S

Sally Green

Thanks again fro your help. I have changed that now. Think I might have
tried that but I do'nnt really know what I'm doing here.

it hangs up on the document name. The new field FullDocName is a text data
type and in the field I have not included the quotes around the full name.
ie I have entered C:\My Documents\TestDoc1. The code correctly identifies
this as "C:\My Documents\TestDoc1". I've had problems in the past with
folder names that include spaces, ie My Documents and had to shorten this to
MyDocu~1 - could that be the problem?

SG
 
S

Sally Green

Sorry, the error was Error 13 Type Mismatch

Albert D. Kallal said:
I assume you going to change the name of this control to that of what the
actual name of the sub form control you used (note that this name is not
always the same as the underlying sub-form name, we are talking about the
*control* name here that is a sub-form control.

I would be a *great* developer I could read your mind..and actually "know"
the name of the sub-form control on your form...

Your assumption to change the name of the field in your sub-form was a
correct, and good assumption.

Note that the document name (rst!WordDoc) must also include the full
qualified path name to the document for word to correctly open the
document....
 
A

Albert D. Kallal

it hangs up on the document name. The new field FullDocName is a text
data
type and in the field I have not included the quotes around the full name.
ie I have entered C:\My Documents\TestDoc1. The code correctly identifies
this as "C:\My Documents\TestDoc1". I've had problems in the past with
folder names that include spaces, ie My Documents and had to shorten this
to
MyDocu~1 - could that be the problem?

Great suggestion...I just decide to try the code, and I noticed that I have
to convert the document name to a string

eg:

Set WordDoc = wordapp.Documents.Open(CStr(rst!WordField))

You can either do the above, or simply declare a string variable such as


dim strDocName as string

And, then in the code loop go:

strDocName = rst!YouWordFieldGoesHere
Set WordDoc = wordapp.Documents.Open(strDocName)

So, note that spaces in the actual resulting field does NOT matter, but it
does seem that you can't "pass" a field name direclty to the word code...it
has to be a string value....

You can also use:

Set WordDoc = wordapp.Documents.Open(rst!YourFieldName.Value)
 
A

Albert D. Kallal

Sally Green said:
Sorry, the error was Error 13 Type Mismatch

see my other post, the code should look like now:

Dim rst As DAO.Recordset
Dim wordapp As Object
Dim WordDoc As Object ' one instance of a word doc


Set wordapp = CreateObject("Word.Application")

Set rst = Me.child1_test.Form.RecordsetClone

rst.MoveFirst
Do While rst.EOF = False
Set WordDoc = wordapp.Documents.Open(rst!WordDocf.Value)
WordDoc.PrintOut
WordDoc.Close (False)
rst.MoveNext
Loop

wordapp.Quit <--- this is new...
Set wordapp = Nothing
Set WordDoc = Nothing
 

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