-
Header & Footer
Hello,
I am using the below code to insert a word document into an existing document. The code executes but my the Header/Footer are on the document being inserted are lost. Is there a way to retain the header/footer when doing something along these lines?
Selection.MoveUp Unit:=wdLine, Count:=75
Selection.InsertFile FileName:="C:\MS Word Template.docx", ConfirmConversions:=False, _
Link:=False, Attachment:=False
With Selection.Font
.Name = "Arial"
.Size = 11
End With
I am relatively new to VBA and would really appreciate any insight into this. I have researched this issue but unfortunately have not come across any working solutions. I appreciate any help anyone has to offer.
-
You would have to insert a section break in order for the inserted file headers and footers to be applied. If you are just appending an inserted file to the end of and existing file it could be as simple as this:
Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
MsgBox ThisDocument.Path
Dim lngIndex As Long
Dim oSect As Section
Set oSect = ActiveDocument.Sections.Add(Selection.Range, wdSectionNewPage)
For lngIndex = 1 To 3
oSect.Headers(lngIndex).LinkToPrevious = False
oSect.Footers(lngIndex).LinkToPrevious = False
Next lngIndex
oSect.Range.InsertFile FileName:=ThisDocument.Path & "\Document 2.docm", ConfirmConversions:=False, _
Link:=False, Attachment:=False 'Revise for your file name.
lbl_Exit:
Exit Sub
End Sub
-
Using this I was able to have it retain the Header & Footer. Thank you so much for that! The one issue I do see using this is that a single blank page is inserted before the Inserted File and 3 blank pages are appearing afterwards. Do you know of a way to avoid this?
-
That was not the case on the simple test I ran here so without seeing your two files I can't really say.
-
I was able to remove the 3 blank pages from the end by editing the file I was inserting. The blank page in the beginning remains but I used the following code to help with that.
Dim oRng As Range
Set oRng = ActiveDocument.Sections(1).Range
oRng.MoveStart wdCharacter, -1
oRng.Delete
It seems to be working now! Thank you for your help, Greg!