Share via

Combine differently formatted Word files preserving layout

Anonymous
2013-07-31T12:42:27+00:00

Title says it. A tall order, I know. Is it even possible? Been trying a few convoluted methods and gotten close but not quite. One way that almost works is using a macro to open each component file, add a section break at the end, store its margins, close it, and then use Selection.InsertFile to add it to the blob and reapply its margins. This kinda works except the header/footer content and properties (alternation, etc.) persist from the prior document in the stack despite setting LinkToPrevious to False.

Obviously this is a major (maybe foolish?) exercise and I'm not done experimenting, but has anyone else attempted this in earnest & come out with any lessons (including 'forget it!')? And yes, I know the One True Answer is probably PDFing but I'm not actually the one who dreamed up this quest, the goal of which is to have a mega-document that's relatively easier to edit or comment than a PDF would be.

I do notice I get different results from setting the Link arg to True or False. Linking, which seems preferable to keep the file size down, blanks out all headers & footers after the first doc regardless of other gyrations.

Code is below. Any clues appreciated.

Sub Test()

Dim fname As String, i As Long, FI() As String

Dim dx As Document, d As Document, newdoc As Document

Dim sek As Section, hf As HeaderFooter

Dim lm As Single, rm As Single, tm As Single, bm As Single

ReDim FI(50)

fname = Dir$("C:\Test\*.docx")

Do While fname <> "": i = i + 1: FI(i) = "C:\Test" & fname: fname = Dir$: Loop

ReDim Preserve FI(i)

For i = 1 To UBound(FI)

    If i = 1 Then

        Set newdoc = Documents.Open(FI(i))

    Else

        'store margin info

        Set dx = Documents.Open(FI(i), Visible:=False)

        lm = dx.Sections(1).PageSetup.LeftMargin

        rm = dx.Sections(1).PageSetup.RightMargin

        tm = dx.Sections(1).PageSetup.TopMargin

        bm = dx.Sections(1).PageSetup.BottomMargin

        dx.Close 0

        'insert file at end

        newdoc.Activate

        Selection.EndKey wdStory

        Selection.Paragraphs(1).Range.InsertFile FI(i), , , False

        With Selection.Sections(1).PageSetup

            .TopMargin = tm

            .BottomMargin = bm

            .LeftMargin = lm

            .RightMargin = rm

        End With

        If i > 1 Then

            For Each hf In Selection.Sections(1).Headers

                hf.LinkToPrevious = False

                DoEvents

            Next

            For Each hf In Selection.Sections(1).Footers

                hf.LinkToPrevious = False

                DoEvents

            Next

        End If

    End If

Next i

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
2014-11-06T08:54:54+00:00

Using code such as the following preserves the headers\footers\pagesetup, etc. in the documents that are inserted:

Dim ADoc As Document

Dim target As Document

Dim docRange As Range

Dim targetRange As Range

Set target = ActiveDocument

Set ADoc = Documents.Open("C:\Users\Doug\Downloads\AAA\Alana\DocA.docx")

Set docRange = ADoc.Range

docRange.Collapse wdCollapseEnd

docRange.InsertBreak Type:=wdSectionBreakNextPage

Set targetRange = target.Range

targetRange.Collapse wdCollapseEnd

Set docRange = ADoc.Range

targetRange.FormattedText = docRange.FormattedText

ADoc.Close wdDoNotSaveChanges

Set ADoc = Documents.Open("C:\Users\Doug\Downloads\AAA\Alana\DocB.docx")

Set docRange = ADoc.Range

docRange.Collapse wdCollapseEnd

docRange.InsertBreak Type:=wdSectionBreakNextPage

Set targetRange = target.Range

targetRange.Collapse wdCollapseEnd

Set docRange = ADoc.Range

targetRange.FormattedText = docRange.FormattedText

ADoc.Close wdDoNotSaveChanges

Was this answer helpful?

0 comments No comments

24 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-08-01T00:29:15+00:00

    Have you tried using select the whole document, Copy, Paste special, Microsoft Word Document Object, paste link into the collection document.

    That way the the content is actually maintained in it's original file, with configuration in the original file.

    Test that type of paste. If it works, try recording a macro while doing it to get the required macro code.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-08-01T00:14:08+00:00

    Hi Doug. I know about PrimoPDF, but unfortunately the final doc does need to be editable.

    Was this answer helpful?

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2013-07-31T23:53:24+00:00

    If the final document does not need to be edited, consider using the free PrimoPDF convertor, which will install itself as a printer and print each of the documents in the order required using the same filename for the pdf that will be created each time and accepting the Append, rather than overwrite option.

    Was this answer helpful?

    0 comments No comments