Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    applying templates to existing documents


    I have been searching for days on the internet trying to find a solution to my problem and have come up with nothing so I'm really hoping someone out there can help!
    I have multiple existing documents that I need to be able to apply a template to. The template contains a header and footer image so the normal templates/addins way will not work. Is there any type of VB code that I can write to make this possible or am I just out of luck?

    I appreciate ANY help!

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Melbourne, Victoria, Australia
    Thanked 295 Times in 267 Posts
    Applying a template won't change any content.

    If you need to change the h/f then I would firstly add them as building blocks in the template. Then I use code to replace the headers and footers with those building blocks. The following code shows you how it might be done.
    Function ReplaceHFwithBB(aRng As Range, sATE As String)
      On Error GoTo errCatch
      ActiveDocument.AttachedTemplate.AutoTextEntries(sATE).Insert Where:=aRng, RichText:=True
      aRng.Collapse Direction:=wdCollapseEnd
      aRng.Delete   'get rid of last paragraph mark
      Exit Function
      MsgBox "Error: " & Err.Number & vbCr & Err.Description
      Exit Function
    End Function
    Sub ChangeHF()
      Dim iSect as Integer
      For iSect = 1 To ActiveDocument.Sections.Count
          ActiveDocument.Sections(iSect).PageSetup.DifferentFirstPageHeaderFooter = False
          ActiveDocument.Sections(iSect).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
          ActiveDocument.Sections(iSect).Footers(wdHeaderFooterPrimary).LinkToPrevious = False
          ReplaceHFwithBB ActiveDocument.Sections(iSect).Headers(wdHeaderFooterPrimary).Range, "MyHeader"
          ReplaceHFwithBB ActiveDocument.Sections(iSect).Footers(wdHeaderFooterPrimary).Range, "MyFooter"
      Next iSect
    End Sub
    Last edited by Andrew Lockton; 2013-04-30 at 19:48. Reason: Repaired the aDoc/ActiveDocument issue
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    srobbins (2013-04-28)

  4. #3
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Sun Prairie, Wisconsin, Wisconsin, USA
    Thanked 128 Times in 123 Posts
    Otherwise, simply create a new document based on the template and paste your content into the new document.
    Charles Kyle Kenyon
    Madison, Wisconsin

Posting Permissions

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