Consulting

Results 1 to 13 of 13

Thread: Combining multiple word documents into one

  1. #1

    Combining multiple word documents into one

    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.
    Last edited by Aussiebear; 04-12-2023 at 06:04 PM. Reason: Adjusted the code tags

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    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.
    Last edited by Aussiebear; 04-12-2023 at 06:05 PM. Reason: Adjusted the code tags

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    ' 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.
    Last edited by Aussiebear; 04-12-2023 at 06:07 PM. Reason: Adjusted the code tags

  6. #6
    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?

  7. #7
    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.

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    Last edited by Aussiebear; 04-12-2023 at 06:08 PM. Reason: Adjusted the code tags
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    What happens when you use InsertFile?

  10. #10
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    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
    Last edited by Aussiebear; 04-12-2023 at 06:09 PM. Reason: Adjusted the code tags

  11. #11
    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.

  12. #12
    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.

  13. #13
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •