Consulting

Results 1 to 9 of 9

Thread: Merge two Headers/Footers

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    75
    Location

    Merge two Headers/Footers

    Hi All

    I've 2 documents:
    1- template with fixed header and footer
    2- any document with different header and footer

    I need to:

    - move the header of the template to the existing document and put it before (up) to the existing one
    - move the footer of the template to the existing document and put it after (down) to the existing one

    I need a Word macro doing this pleaseeeeeeeeeeeeeee

  2. #2
    Without knowing exactly what is in the two documents it is difficult to judge, but the following will work for a single section. Open the document you want to process and run the macro. Select the template at the prompt.

    Option Explicit
    'Graham Mayor - https://www.gmayor.com - Last updated - 20 Sep 2020 
    Sub Macro1()
    Dim oDoc As Document, oSource As Document
    Dim oRng As Range
    Dim sName As String
    Dim fDialog As FileDialog
        Set oDoc = ActiveDocument
        On Error GoTo err_Handler
        Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
        With fDialog
            .TITLE = "Select the source template"
            .AllowMultiSelect = False
            .Filters.Clear
            .Filters.Add "Word documents", "*.doc,*.docx,*.docm"
            .InitialView = msoFileDialogViewList
            If .Show <> -1 Then GoTo err_Handler:
            sName = fDialog.SelectedItems.Item(1)
            Set oSource = Documents.Open(sName)
            If oSource.Sections(1).Headers(wdHeaderFooterFirstPage).Exists Then
                If oDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Exists Then
                    Set oRng = oDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range
                    oRng.Collapse 1
                    oRng.FormattedText = oSource.Sections(1).Headers(wdHeaderFooterFirstPage).Range.FormattedText
                End If
            End If
            If oSource.Sections(1).Headers(wdHeaderFooterPrimary).Exists Then
                If oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Exists Then
                    Set oRng = oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
                    oRng.Collapse 1
                    oRng.FormattedText = oSource.Sections(1).Headers(wdHeaderFooterPrimary).Range.FormattedText
                End If
            End If
            If oSource.Sections(1).Headers(wdHeaderFooterEvenPages).Exists Then
                If oDoc.Sections(1).Headers(wdHeaderFooterEvenPages).Exists Then
                    Set oRng = oDoc.Sections(1).Headers(wdHeaderFooterEvenPages).Range
                    oRng.Collapse 1
                    oRng.FormattedText = oSource.Sections(1).Headers(wdHeaderFooterEvenPages).Range.FormattedText
                End If
            End If
            If oSource.Sections(1).Footers(wdHeaderFooterFirstPage).Exists Then
                If oDoc.Sections(1).Footers(wdHeaderFooterFirstPage).Exists Then
                    Set oRng = oDoc.Sections(1).Footers(wdHeaderFooterFirstPage).Range
                    oRng.Collapse 0
                    oRng.FormattedText = oSource.Sections(1).Footers(wdHeaderFooterFirstPage).Range.FormattedText
                End If
            End If
            If oSource.Sections(1).Footers(wdHeaderFooterPrimary).Exists Then
                If oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Exists Then
                    Set oRng = oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
                    oRng.Collapse 0
                    oRng.FormattedText = oSource.Sections(1).Footers(wdHeaderFooterPrimary).Range.FormattedText
                End If
            End If
            If oSource.Sections(1).Footers(wdHeaderFooterEvenPages).Exists Then
                If oDoc.Sections(1).Footers(wdHeaderFooterEvenPages).Exists Then
                    Set oRng = oDoc.Sections(1).Footers(wdHeaderFooterEvenPages).Range
                    oRng.Collapse 0
                    oRng.FormattedText = oSource.Sections(1).Footers(wdHeaderFooterEvenPages).Range.FormattedText
                End If
            End If
        End With
    lbl_Exit:
        Set oDoc = Nothing
        Set oSource = Nothing
        Set oRng = Nothing
        Exit Sub
    err_Handler:
        Err.Clear
        GoTo lbl_Exit
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    75
    Location
    Dear Graham

    You are
    Genius and thanks a lot for helping me.

    I tested the macro and it works but the 2 headers/footers are overlapped.

    I want to template header footer to be up and the opening document to be bottom.

    I'll share the 2 sample documents with you.

    Thanks again.

  4. #4
    VBAX Regular
    Joined
    Dec 2015
    Posts
    75
    Location
    Here you are the 2 documents @ https://mega.nz/folder/PPhHALgb#JSOvzwD22cRrqZ8GHoaebA

    and these are the correct view after i edited it manually:

    header.jpg

    footer.jpg

  5. #5
    VBAX Regular
    Joined
    Dec 2015
    Posts
    75
    Location
    @Graham -- Any news sir ?

  6. #6
    The problem you have is not an insignificant one.You have a mixture of in-line graphics and floating graphics in your headers and footers. Floating graphics are in the graphics layer of the document which is why they overlap, whereas inline graphics behave like text and are easier to deal with. I cannot currently think of a simple solution.

    Why are you adopting this approach? Wouldn't it be simpler to create the combined header and footer that you require, as in your illustration, save them as autotext entries and apply them to your document - or better still create new documents from the template with the header/footer that you actually require.?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Dec 2015
    Posts
    75
    Location
    Dear Graham

    Can you adjust the Macro to be flat and work with any documents to do the below:

    - Move the Template document header/footer to the Original document and keep them together but:
    * The template header will be above the original header
    * The template footer will be below the original header


  8. #8
    VBAX Regular
    Joined
    Dec 2015
    Posts
    75
    Location
    Dear Graham

    Can you adjust the Macro to be flat and work with any documents to do the below:

    - Move the Template document header/footer to the Original document and keep them together but:
    * The template header will be above the original header
    * The template footer will be below the original header

  9. #9
    VBAX Regular
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    14
    Location
    If you can make both headers and footers graphic images that are inline with text Graham's code should work. The one that is currently mixed could be placed inside a textbox which was then set to be inline with text.

    Read what Graham told you.
    Think about his proposal.
    Repeating your question does not change anything.

    Here are links to your documents with what I believe to be graphical H & F elements in line with text. Give it a try.
    https://www.dropbox.com/s/fmg5xy54y4gyu4j/ooo.docx?dl=0
    https://www.dropbox.com/s/bjx0zd9ziz9h8fd/sample%20with%20h%26f.docx?dl=0



Posting Permissions

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