Opening word, printing a .doc file, then closing word all from another program

B

Brian Beck

I have Office 2003 running in Windows XP and I've got an Access database
that I need to print different files from. Essentially I've got a case
statement that looks at the extension of a file and then opens the
appropriate program, prints the file, and then closes that program. It's
quite easy to do for Adobe Acrobat with a command line switch, but I'm not
finding anything like that for Microsoft Word.

Is is possible with VBA to programmatically open up a Word document, print
that file, and then close Word?...and all from a VBA script in Access?

-Brian Beck
 
J

Jean-Guy Marcil

Brian Beck was telling us:
Brian Beck nous racontait que :
I have Office 2003 running in Windows XP and I've got an Access
database that I need to print different files from. Essentially I've
got a case statement that looks at the extension of a file and then
opens the appropriate program, prints the file, and then closes that
program. It's quite easy to do for Adobe Acrobat with a command line
switch, but I'm not finding anything like that for Microsoft Word.

Is is possible with VBA to programmatically open up a Word document,
print that file, and then close Word?...and all from a VBA script in
Access?

Here is one of many ways (you will need to set a reference to the Word
Object Model):

'_______________________________________
Dim appWord As Word.Application
Dim docPrint As Word.Document

Set appWord = New Word.Application

With appWord
Set docPrint = .Documents.Open("C:\Test\Doc2.doc")
With docPrint
.PrintOut
.Close 0
End With
.Quit
End With

Set docPrint = Nothing
Set appWord = Nothing
'_______________________________________

Note that in this case the whole thing is invisible, if you want to see what
is gong on the screen, use

With appWord
.Visible = True
Set docPrint = .Documents.Open("C:\Test\Doc2.doc")

instead of

With appWord
Set docPrint = .Documents.Open("C:\Test\Doc2.doc")

You may want to look into CreateObject and GetObject instead so that you can
check if the Word application is already running. I believe that the VBA
on-line help for these two functions is quite useful.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.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