PDA

View Full Version : Combining multiple word documents into one



presence76
03-15-2006, 03:29 PM
I have some code that prints out multiple word documents. It works, but when we did a full volume test, which is about 2,000 a day, the printer began skipping pieces of documents. We ramped up the memory on the printer, did not work. This VBA code is executed within an Access DB. However, when I execute it from a macro within a word document, it prints properly, but way too slow. So, I want to try to merge all the documents in to one and then try to print that.

Does anyone know how to do that within the FSO environ. I have been trying for a few hours now.


Dim wordApp As New Word.Application
Dim myDoc As Word.Document
Dim myDoc2 As Word.Document
wordApp.Visible = False
wordApp.ScreenUpdating = False
wordApp.DisplayAlerts = False
Const TARGET_FOLDER_ONEPAGE As String = "P:\Clients\Vanguard\Finance\testing\onepagedocs\"
With wordApp
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.Merge
myDoc.Close False
End If
Next f
wordApp.Quit
End With



I had the screenupdating off to try to minimize overhead. If anyone can show me how or point me in the write direction, it would be greatly appreciated. Thanks.

fumei
03-15-2006, 03:59 PM
You declare myDoc2...but you never do anything with that. Where is the second doc?

I assume you want to take the FIRST doc and merge the other docs that one? You need to do a second loop.

lucas
03-15-2006, 09:09 PM
Jake has a knowledge base entry that does this. It doesn't use fso and I don't know how it will work with the volumes your talking about but here's a link to it if your interested. Works great for my needs.
http://vbaexpress.com/kb/getarticle.php?kb_id=214

presence76
03-16-2006, 05:37 AM
Thanks for the replies. Here is what I have. I have tried all kinds of different variations on the With Mydoc2 section but none work.


wordApp.Visible = False
wordApp.ScreenUpdating = False
wordApp.DisplayAlerts = False
Set WordApp2 = CreateObject("Word.Application")
Set myDoc2 = WordApp2.Documents.Add
Const TARGET_FOLDER_ONEPAGE As String = "P:\Clients\Vanguard\Finance\testing\onepagedocs\"
With wordApp
'myDoc2 = wordApp.Documents.Add
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)
With myDoc2
InsertFile.myDoc
End With
myDoc.Close False
End If
Next f
myDoc2.SaveAs ("P:\Clients\Vanguard\Finance\testing\onepagedocs\allonepages.doc")
wordApp.Quit
End With
WordBasic.DisableAutoMacros False



Is the with mydoc2 the wrong approach? Any help is greatly appreciated.

fumei
03-16-2006, 12:38 PM
' not sure what the heck is going on with WordApp2
' and wordapp - are you making two instances????
Set WordApp2 = CreateObject("Word.Application")
' OK, make a new document
Set myDoc2 = WordApp2.Documents.Add
' please note the use of the underscore character
' it is appreciated if you would use it! Thanks.
Const TARGET_FOLDER_ONEPAGE As String = _
"P:\Clients\Vanguard\Finance\testing\onepagedocs\"
With wordApp
Set Fso = New FileSystemObject
Set fldr = Fso.GetFolder(TARGET_FOLDER_ONEPAGE)
For Each f In fldr.Files
If Right(f.Name, 4) = ".doc" Then
myDoc2.Merge Filename:= f.path
End If
Next f
myDoc2.SaveAs _
("P:\Clients\Vanguard\Finance\testing\onepagedocs\allonepages.doc")
wordApp.Quit
End With
Set fso = Nothing
' etc etc etc

This should take each f in fldr (that is a .doc) and merge it to myDoc2. However, I would definitely check to see if you need to put a page break between documents (likely). In which case, just add it to myDoc2 between the merges.

presence76
03-16-2006, 01:36 PM
Yes, I did get off the 2 word applications theory a while back and should have posted that. I modified my code with yours but it produces a document that is totally blank. I'm afraid that because these documents are "locked" when they are sent to us, that this sort of manipulation is not possible. Does that sound correct?

presence76
03-16-2006, 02:22 PM
I tried this using my own word documents, unlocked, with only one line of text and it did not work - I got the same result - an empty word file. When I walk it through the debugger, it locks up on the Merge line. I thought I disliked Adobe. PDF's are a dream compared to Word. Any replies would be greatly appreciated.

mdmackillop
03-16-2006, 03:42 PM
InsertFile works for me from an Excel file


Option Explicit
Sub Combine()
'This code requires a referece to the Word object model
'and windows scripting runtime
Dim Appword As New Word.Application
Dim fso, fldr, f
Set Appword = CreateObject("Word.Application")
Appword.Documents.Add
Set fso = New FileSystemObject
Set fldr = fso.GetFolder("C:\AAA")
For Each f In fldr.Files
If Right(f.Name, 4) = ".doc" Then
Appword.Selection.InsertFile Filename:="C:\AAA\" & f.Name
End If
Next f
Appword.Visible = True
End Sub

fumei
03-16-2006, 06:04 PM
What happens when you use InsertFile?

geekgirlau
03-16-2006, 09:29 PM
My understanding is that the reason you want to merge the files together is because you were unsuccessful in printing them individually - is this correct? If so, what about setting a delay -



Dim wordApp As New Word.Application
Dim myDoc As Word.Document
Dim myDoc2 As Word.Document ' DON'T NEED THIS!
Dim dtmStart As Date
Const TARGET_FOLDER_ONEPAGE As String = _
"P:\Clients\Vanguard\Finance\testing\onepagedocs\"
With wordApp
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = False
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)
' date/time stamp
dtmStart = Now()
myDoc.PrintOut
myDoc.Merge ' WHY DO YOU NEED THIS ONE?
' set delay here -
'I've used 2 minutes, may need to experiment
Do
DoEvents
Loop Until DateDiff("n", dtmStart, Now()) > 2
myDoc.Close False
End If
Next f
End With
wordApp.Quit

presence76
03-20-2006, 06:19 AM
Sorry for the late reply. Insertfile works correctly except there are page counters on each document (page 1 of 2) that are in the headers that dissappear when I use insertfile.

I have tested with the delay and unless I put in an obnoxiously high pause like ten seconds, we end up with documents printed that ONLY contain signatures on them. No matter how much horsepower we throw at it (a gig of ram on the machine running it and the printer cards quadrupled), word is just brutally inefficient for high volume printing. At ten seconds a piece for 3,000 a day, that is too much spool time.

We are going with splitting up the job to multiple printers at the same time. The macro to print only works when running it from within a word document - if I run it from the Access database, it has all these effieciency problems.

The other option is to create PDF's and then combine them.

If I continue to test with the Insertfile option and try to fix the headers, I will report back with results.

As always, thanks for everyone's contributions.

Annodomini2
03-22-2006, 04:44 AM
Don't get me wrong I'm not telling you how to do business, but given you're printing 3000 copies a day, have you considered either emailing as a pdf?

Or if this is not an option using a commercial printers (i.e. a printing company) as given the number of issues you appear to be having, this may be faster and/or cheaper given the volumes in which you are printing.

presence76
03-22-2006, 05:50 AM
Actually, we are a high volume print shop. This is a case of us accomodating a format we don't normally get, we usually get data and build the app ourselves using Dialogue by Exstream. Lets face it, Word was not designed for variable high volume print. In addition each document has a signature on it so they are fairly large for one or two pages(around 83k). Yes we are considering coverting to a PDF and then combining those to one PDF and printing that. We have a program that converts word to pdf and I am currently working on executing this from VBA with the Shell command. When I get it working, I will post. Thanks for the reply.