PDA

View Full Version : Print multiple word docs using VBA



presence76
02-22-2006, 01:42 PM
I am a programmer with moderate VBA experience but most of it pertains to Excel. I have a directory that is populated daily with about 1,500 word documents. The only constant in the names is the .doc extension. If I try to highlight all the files and print, my system locks (no matter how much horsepower my PC or the network has). The only way to print them is to manually open up each one. They are one or sometimes 2 page documents.

Is there a way to print these word documents using VBA without locking up the network or the PC it's executing on? The printer is networked.

Thanks in advance for any help.

Killian
02-22-2006, 02:27 PM
Hi and welcome to VBAX :hi:

You need to open the documents one at a time to control the resources
A simple way to to that is to set up a FileSystemObject, set a target folder and loop through its matching files (*.doc).
Word will be fully occupied for a while with 1500 docs and that will be noticable on the workstations. Network bandwitdh will be used up however you do it, unless you choose to pause in the loop.

So first, add a reference to the Microsoft Scripting Runtime (in the VBE, Tols>References and secelt it from the list) then this routine should workSub PrintAll()

Dim fso As FileSystemObject
Dim fldr As Folder
Dim f As File
Dim myDoc As Document
Const TARGET_FOLDER As String = "C:\Temp\"

Set fso = New FileSystemObject
Set fldr = fso.GetFolder(TARGET_FOLDER)
For Each f In fldr.Files
If Right(f.Name, 4) = ".doc" Then
Set myDoc = Documents.Open(TARGET_FOLDER & f.Name)
myDoc.PrintOut
myDoc.Close False
' you could append a log file here
End If
Next f

End Sub

presence76
02-22-2006, 02:42 PM
Unbelievable. You rock.

Now, I only got it to work with a local printer and it does tie up the PC running it, but thats probably how we are going to have to have to function right?

Thanks alot!!!!!

Killian
02-22-2006, 02:56 PM
You're welcome :-)

Something will have to run Word, open each doc and spool it to the print queue.
If you want to get giddy on control of the PC that's doing it, you could use WinAPI functions to get hold of the Word application thread and limit it's priority. A neat trick, I suppose, but it'll just take longer - the processer hours will be the same. It would only be worth it if you desparately need that PC and even then it might not be too quick (lots of hard drive activity making the print file)

mdmackillop
02-22-2006, 03:34 PM
I would have some concern over the the process fouling up with such a huge print job. Here's a variation of Killian's code which should print in defined batches at intervals.
Good luck with it either way.
Regards
MD
Dim MyList() As String
Dim i As Long, j As Long
Const Batch As Long = 3
Const TARGET_FOLDER As String = "C:\AAA\"
Const Wait As String = "00:00:10"

Sub PrintAll()

Dim fso As FileSystemObject
Dim fldr As Folder
Dim f As File
Dim myDoc As Document

ReDim MyList(2000)

Set fso = New FileSystemObject
Set fldr = fso.GetFolder(TARGET_FOLDER)
For Each f In fldr.Files
If Right(f.Name, 4) = ".doc" Then
i = i + 1
MyList(i) = f.Name
End If
Next f
ReDim Preserve MyList(i)
PrintTime
End Sub
Sub PrintTime()
Application.OnTime When:=Now + TimeValue(Wait), _
Name:="DoPrint"
End Sub
Sub DoPrint()
Do
j = j + 1
Set myDoc = Documents.Open(TARGET_FOLDER & MyList(j))
myDoc.PrintOut
myDoc.Close False
If j = i Then Exit Sub
Loop Until j Mod Batch = 0

PrintTime
End Sub

presence76
03-14-2006, 08:48 AM
Thanks for the additional post on intervals. I got the original code to work just fine. However,now I have taken this macro and put it into an Access database, so I can develop a form to interact with the user and they can specify which documents to print and so on. The code will not compile within Access.

Dim myDoc as Document
const TARGET_FOLDER_ONEPAGE As String = "P:\Clients\Vanguard\Finance\testing\onepagedocs\"
Set Fso = New FileSystemObject
Set fldr = Fso.GetFolder(TARGET_FOLDER_ONEPAGE)
For Each f In fldr.Files
If Right(f.Name, 4) = ".doc" Then
Set myDoc = Documents.Open(TARGET_FOLDER_ONEPAGE & f.Name)
myDoc.PrintOut
myDoc.Close False
End If
Next f

When I compile it tells me that "method or data member not found" on the myDoc.Printout statement. Am I missing some reference to Word??
One note, at this point in the code, I have opened and closed a text file, as well as created a XLS book - but I closed those apps down.

Do I need to specify word somehow? Thanks in advance.

presence76
03-14-2006, 09:22 AM
I changed


Dim myDoc as document

to

Dim myDoc as Word.Document