PDA

View Full Version : Merge two Headers/Footers



baset
09-19-2020, 11:52 AM
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

gmayor
09-20-2020, 04:02 AM
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

baset
09-20-2020, 05:43 AM
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.

baset
09-20-2020, 06:24 AM
Here you are the 2 documents @ https://mega.nz/folder/PPhHALgb#JSOvzwD22cRrqZ8GHoaebA

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

27157

27158

baset
09-21-2020, 02:21 AM
@Graham -- Any news sir ?

gmayor
09-21-2020, 03:52 AM
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.?

baset
09-28-2020, 10:42 AM
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

baset
09-29-2020, 09:48 AM
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

Chas Kenyon
09-30-2020, 08:14 PM
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


(https://www.dropbox.com/s/bjx0zd9ziz9h8fd/sample%20with%20h%26f.docx?dl=0)