Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-04-2016, 02:25 AM
mgirving mgirving is offline Inserting new footer into existing documents Windows XP Inserting new footer into existing documents Office 2010 64bit
Novice
Inserting new footer into existing documents
 
Join Date: May 2016
Posts: 1
mgirving is on a distinguished road
Default Inserting new footer into existing documents

Our company has changed the look of its documents and one of the changes is that documents now have a second page footer instead of a second page header and the footer content is also different.

I recorded the macro below to enable users to update any documents they are already working with on the 'change over day' to be compliant with the new look (so that users don't have to start from scratch with their work in one of the new look templates but can continue working on existing documents).

When the macro is run it produces variable results. Most often, instead of the new footer appearing in the footer, it appears in the body of the documents. I have read forum posts that explain that a recorded macro is unreliable and it is best done through VBA.

I would be grateful for any assistance in how I can convert this into a VBA code.

Ideally, I would like to set it up so that it can be run on a document regardless of how many pages it currently is. If it is currently only a one page letter, the macro would still set up the new footer in case the user creates a second page of the letter after typing some more content.

Thank you for any assistance.


' New Footer Macro
'
'
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILLIN ""Insert addressee's name"" ", PreserveFormatting:=True
Selection.TypeText Text:=" | "
Selection.InsertDateTime DateTimeFormat:="d MMMM yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _


InsertAsFullWidth:=False
Selection.TypeText Text:=vbTab
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic ", PreserveFormatting:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Page "
Selection.ParagraphFormat.TabStops(CentimetersToPo ints(7.62)).Position = _
CentimetersToPoints(7.62)
Selection.ParagraphFormat.TabStops.ClearAll
ActiveDocument.DefaultTabStop = CentimetersToPoints(1.2)
Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(16.5) _
, Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
Selection.HomeKey Unit:=wdLine
Selection.TypeParagraph
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.TypeBackspace
End Sub
Reply With Quote
  #2  
Old 05-04-2016, 03:52 AM
gmayor's Avatar
gmayor gmayor is offline Inserting new footer into existing documents Windows 10 Inserting new footer into existing documents Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following will remove the Primary Header content and add the footer to the Primary Footer. The first page header and footer are unaffected.

Note the macro addresses only the first section of the document. Letters don't usually have multiple sections, but if yours do the additional sections need to be addressed.
Code:
Option Explicit

Sub ChangeHeaderFooter()
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim strName As String
Dim orng As Range
    strName = InputBox("Enter addressee's name")
    ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
    Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
    oHeader.Range.Text = ""
    Set oFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
    Set orng = oFooter.Range
    orng.ParagraphFormat.TabStops.ClearAll
    orng.ParagraphFormat.TabStops.Add _
            Position:=CentimetersToPoints(16.5), _
            Alignment:=wdAlignTabRight, _
            Leader:=wdTabLeaderSpaces
    orng.Text = strName & " | " & Format(Date, "d MMMM yyyy") & vbTab & "Page "
    orng.Collapse 0
    orng.Fields.Add Range:=orng, Type:=wdFieldPage, PreserveFormatting:=False
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting new footer into existing documents edit date in footer of existing slides eNGiNe PowerPoint 6 05-27-2015 05:39 AM
Inserting new footer into existing documents Inserting Table of Figures Insists on Overwriting Existing sleake Word 8 09-15-2013 02:42 PM
How to insert full documents into existing word document Laraak Word 1 03-07-2013 11:59 PM
Inserting section between existing sections GoneBirding Word 0 10-18-2012 03:01 PM
Assigning templates to existing documents. Update Styles Enmasse. bannerdog Word 1 02-28-2012 03:53 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:57 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft