Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Oct 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Combining Word Documents (Word 97)

    I am looking for a way to combine all of the Word 97 files in a directory into a single Word 97 file. Word seems not to offer any solution, let alone an elegant one.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Combining Word Documents (Word 97)

    Perhaps somebody else will come up with a non-VBA solution. Here is a macro you can adapt to your needs:

    Sub CombineDocs()
    Dim doc As Document
    Dim strFileName As String
    Dim strPath As String
    Set doc = Documents.Add
    strPath = InputBox("Enter folder")
    If strPath = "" Then Exit Sub
    strFileName = Dir(strPath & "*.doc")
    Do While strFileName <> ""
    Selection.InsertFile strFileName
    Selection.InsertBreak wdSectionBreakNextPage
    strFileName = Dir
    Loop
    End Sub

    The macro will prompt you to enter the path of the folder. After each document, a section break (next page) is inserted. You can change the instruction

    Selection.InsertBreak wdSectionBreakNextPage

    if you don't like that.

  3. #3
    New Lounger
    Join Date
    Oct 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Combining Word Documents (Word 97)

    Hans,

    Thank you the quick response and for the solution.

    I am having difficulty getting it to run. Each time I enter a directory name, the code finds the first file, and an error message pops up saying:

    Run time error '5174'
    This file could not be found.
    Try one or more of the following:
    Check the spelling of the name of the document.
    Try a different file name.
    (AUG97.DOC)

    The file name in () is always the first filename in the directory. I have tried a small directory with only 2 files, 1 file, etc., and the same error message appears.

    What am I doing wrong?

    Don

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Combining Word Documents (Word 97)

    I can't work out the reasoning on Hans solution so I will provide my own method
    <pre>Sub BuildDoc()
    Dim I As Integer
    With Application.FileSearch
    .LookIn = "C:Work"
    .FileName = "*.doc"
    .SearchSubFolders = False
    .Execute
    If .FoundFiles.count > 0 Then
    For I = 1 To .FoundFiles.count
    Selection.text = .FoundFiles(I) & vbCr
    Selection.InsertFile FileName:=.FoundFiles(I), Range:="", _
    ConfirmConversions:=False, Link:=False, Attachment:=False
    Selection.text = vbCr & vbCr
    Next I
    End If
    End With
    End Sub</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Combining Word Documents (Word 97)

    Don,

    Sorry about that. I had only tested it with a path that was already the current directory. It should work if you change the Selection.InsertFile line by

    Selection.InsertFile strPath & "" & strFileName

    But meanwhile, Andrew Lockton has posted a more elegant solution - the user can browse for the folder instead of typing it.

  6. #6
    New Lounger
    Join Date
    Oct 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Combining Word Documents (Word 97)

    Thank you, Andrew. Excellent solution.

  7. #7
    New Lounger
    Join Date
    Oct 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Combining Word Documents (Word 97)

    Thank you, Hans. Works great.

  8. #8
    Star Lounger
    Join Date
    Mar 2001
    Location
    St Georges du Bois, Pays de la Loire
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Combining Word Documents (Word 97)

    Hans,

    Just found your macro when presented with a pile of files to combine...

    Couldn't make Andrew's solution work and I used a tip from the MVP to amend yours to:

    Sub CombineDocs()

    Dim sPath As String
    Dim doc As Document
    Dim strFileName As String

    With Dialogs(wdDialogCopyFile)
    If .Display <> 0 Then

    sPath = .Directory

    Else
    MsgBox "Dialog cancelled"
    End If
    End With

    Set doc = Documents.Add
    strFileName = Dir(sPath & "*.doc")
    Do While strFileName <> ""
    Selection.InsertFile sPath & "" & strFileName
    Selection.InsertBreak wdSectionBreakNextPage
    strFileName = Dir
    Loop

    End Sub

    Worked fine for me - many thanks to you & andrew and maybe this amended version will be of use to someone.

    David

Posting Permissions

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