PDA

View Full Version : Macro to merge linked documents



JPh
08-29-2018, 08:43 AM
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?

macropod
08-29-2018, 04:40 PM
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/showthread.php?51797-Macro-to-merge-mulitple-word-doc-into-one-word-doc&p=354835&viewfull=1#post354835. The macro there handles most differences in page layout and works around any Style/direct formatting inconsistencies.

JPh
08-29-2018, 05:58 PM
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.

macropod
08-29-2018, 06:05 PM
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.

JPh
08-29-2018, 06:30 PM
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.

macropod
08-29-2018, 09:38 PM
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')...

JPh
08-30-2018, 05:28 AM
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