Consulting

Results 1 to 5 of 5

Thread: Header & Footer

  1. #1
    VBAX Newbie
    Joined
    Jul 2019
    Posts
    4
    Location

    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.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,046
    Location
    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:

    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Jul 2019
    Posts
    4
    Location
    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?

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,046
    Location
    That was not the case on the simple test I ran here so without seeing your two files I can't really say.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Newbie
    Joined
    Jul 2019
    Posts
    4
    Location
    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!

Tags for this Thread

Posting Permissions

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