Consulting

Results 1 to 7 of 7

Thread: Macro to merge linked documents

  1. #1
    VBAX Regular
    Joined
    Aug 2018
    Posts
    8
    Location

    Macro to merge linked documents

    In a Word document I have a list of names, each one of them linked to a Word document.

    name1 (linked to file:///C:\Users\...file1.doc)
    name2 (linked to file:///C:\Users\...file2.doc)
    ...

    Is there a way (macro) to parse this list and create a document that contains all the linked documents?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Yes but, without a lot of work to address possible differences in page layout and Style/direct formatting inconsistencies, the result might not be what you want. For a macro to get you started, see: http://www.vbaexpress.com/forum/show...l=1#post354835. The macro there handles most differences in page layout and works around any Style/direct formatting inconsistencies.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Aug 2018
    Posts
    8
    Location
    Thanks for responding.
    I tested the macro:
    Sub MergeDocs()
    Dim rng As Range
    Dim MainDoc As Document
    Dim strFile As String, strFolder As String
    Dim Count As Long
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Pick folder"
    .AllowMultiSelect = False
    If .Show Then
    strFolder = .SelectedItems(1) & Application.PathSeparator
    Else
    Exit Sub
    End If
    End With
    Count = 0
    strFile = Dir$(strFolder & "*.doc") ' can change to .docx
    Do Until strFile = ""
    WordBasic.DisableAutoMacros 1
    If Count = 0 Then
    Set MainDoc = Documents.Add(Template:=strFolder & strFile)
    Count = Count + 1
    Else
    Set rng = MainDoc.Range
    With rng
    .Collapse 0
    If Count > 0 Then
    .InsertBreak Type:=wdSectionBreakNextPage
    .End = MainDoc.Range.End
    .Collapse 0
    End If
    .InsertFile strFolder & strFile
    End With
    End If
    strFile = Dir$()
    WordBasic.DisableAutoMacros 0
    Loop
    MsgBox ("Files are merged")
    lbl_Exit:
    Exit Sub
    End Sub
    It works fine, but the documents, taken from a folder, are in alphabetical order.
    What I'm hoping to accomplish is having the order given in the document list. Is it possible to modify the macro so that it reads the file names from a document as shown in my initial message?
    All the files are in the same format, page layout and style, so I don't need to worry about that.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    As I said, it's:
    a macro to get you started
    Obviously, you'd need to make some changes to have the macro process a list in a document. It's up to you whether you code it so the list has to be selected beforehand or, perhaps, gets it from a defined range without needing anything to be selected.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Aug 2018
    Posts
    8
    Location
    Ideally, I'd like to process a list of names in a document (even better a sublist of highlighted names), extract the .doc files they link to (skipping if there is none), and merge the documents in that order.
    I could, presumably, start learning VBA, and would perhaps get this to work in several months. My impression was that the forum was designed to avoid that.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Forums like this are not a free coding service. And now you're changing the requirements from a list to a highlighted sub-list (and it's not even clear what you mean by 'highlighted')...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular
    Joined
    Aug 2018
    Posts
    8
    Location
    I wrote *even better*, which would have been a second step, not a change. By highlighted I meant selected, the way Word uses it in Find and Replace.
    That said, I understand what you are saying. I got great help on this site already in another thread and was pushing it too far this time.
    All the best,
    Jean

Tags for this Thread

Posting Permissions

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