• Combining Word Documents (Word 97)

    Author
    Topic
    #378246

    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.

    Viewing 0 reply threads
    Author
    Replies
    • #625392

      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.

      • #626522

        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

        • #626542

          I can’t work out the reasoning on Hans solution so I will provide my own method

          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
        • #626622

          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.

          • #626689

            Thank you, Hans. Works great.

          • #636197

            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

    Viewing 0 reply threads
    Reply To: Combining Word Documents (Word 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: