Results 1 to 6 of 6

Thread: [RESOLVED][Word 2010] Insert text in all footers in a document

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    6

    [RESOLVED][Word 2010] Insert text in all footers in a document

    I am trying to insert some standard text in all footers in a given document while retaining any existing footer text. My text should get inserted at the end of any existing text, on a new line. Here's my code so far:

    Public Sub FooterTest()
    Dim oSection As Section
    Dim oHF As HeaderFooter
    Dim ExistingFooterText As String
    For Each oSection In ActiveDocument.Sections
    For Each oHF In oSection.Footers
    FooterEdit
    Next
    Next
    End Sub
    Private Sub FooterEdit()
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph
    Selection.Text = "This is a footer"
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub

    I can't figure out how to get it to loop through the document. This code simply writes "This is a footer" 8 times in the first footer. My sample document has 3 sections (2 pages each), with separate first page footers in each section and no footers are linked, so theoretically, I should only have 6 different footers in the document. I feel it has something to do with wdSeekCurrentPageFooter, but I can't figure out the logic to get it to work.
    Last edited by bflo1127; Apr 22nd, 2011 at 10:06 AM. Reason: Mark as resolved

  2. #2
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: [Word 2010] Insert text in all footers in a document

    Why are you running 2 loops? Just Try:
    vb Code:
    1. Public Sub FooterTest()
    2.     Dim oSection As Section
    3.    
    4.     For Each oSection In ActiveDocument.Sections
    5.         FooterEdit
    6.     Next
    7.    
    8. End Sub
    9.  
    10. Private Sub FooterEdit()
    11.  
    12.     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    13.     Selection.EndKey Unit:=wdStory
    14.     Selection.TypeParagraph
    15.     Selection.Text = "This is a footer"
    16.     ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    17.    
    18. End Sub

    I tested this in 2007, I just made 4 pages and ran this code and it gave me "This is a footer" one time, on each page, in the footer.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    6

    Re: [Word 2010] Insert text in all footers in a document

    Thanks! I'll give that a try and let you know!

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word 2010] Insert text in all footers in a document

    try like
    vb Code:
    1. with thisdocument.sections(1)
    2.   for i = 1 to 3
    3.     .footers(i).range.text = .footers(i).range.text & vbnewline & "some more footer"
    4.   next
    5. end with
    works correctly in 2000, 2003, not tested in later versions, but i believe is still correct
    not necessary to do all sections, change one, change all
    this will change main footer, first page footer and even page footer (if the last 2 are used)
    Last edited by westconn1; Apr 21st, 2011 at 11:41 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    6

    Resolved Re: [Word 2010] Insert text in all footers in a document

    I can't thank you both enough for helping me. I really appreciate the ideas you've given me, but sad to say, neither of your macros worked for me. no_one, your code did what my original post did. Whatever page I was on, it went into that page's footer and wrote the text once for each other footer I had in the document. westconn1, your code didn't work at all. Not sure if it was just a snippet, but I put it in a sub/end sub construct and it just didn't run. Like I said, though, your samples both gave me some good ideas for other things I have to do. I wonder if, in your testings, you left the footers linked to one another. I turned off all links to previous. Sorry if not mentioning that caused some confusion.

    I did find and modify a macro Greg Maxey had posted on another forum. I'll put the code here in case anyone else needs this. It puts my inserted text at the end of any existing macros in a bookmark so I can manipulate it easily in the future.

    Sub DMFooter()
    Dim strMyText As String
    Dim i As Long
    Dim j As Integer
    Dim pFooter As HeaderFooter
    Dim oRng As Word.Range
    Dim strMyFtrType As String
    Dim strMyBMName As String
    strMyText = "more footer text"
    'remember the current view as the text manipulation in the macro
    'changes the view to draft

    Dim initial_active_window_view_type As Integer
    initial_active_window_view_type = ActiveWindow.View.Type
    For i = 1 To ActiveDocument.Sections.Count
    With ActiveDocument.Sections(i)
    For j = 1 To 3
    Set pFooter = .Footers(Choose(j, wdHeaderFooterFirstPage, wdHeaderFooterPrimary, wdHeaderFooterEvenPages))
    pFooter.LinkToPrevious = False
    strMyFtrType = Choose(j, "First", "Primary", "Even")
    strMyBMName = "Bkmk_" & i & "_" & strMyFtrType
    If ActiveDocument.Bookmarks.Exists(strMyBMName) Then
    Set oRng = ActiveDocument.Bookmarks(strMyBMName).Range
    oRng.Text = strMyText
    ActiveDocument.Bookmarks.Add strMyBMName, oRng
    Else
    Set oRng = pFooter.Range
    If oRng.Frames.Count > 0 Then
    If InStr(oRng.Frames(1).Range.Fields(1).Code, "PAGE") > 0 Then
    oRng.Start = oRng.Frames(1).Range.End + 1
    oRng.Collapse wdCollapseStart
    End If
    End If
    If Len(pFooter.Range.Text) > 1 Then
    oRng.InsertAfter vbCr
    End If
    oRng.Collapse wdCollapseEnd
    oRng.Text = strMyText
    ActiveDocument.Bookmarks.Add strMyBMName, oRng
    'select my text to reduce the font size without affecting
    'the rest of the text in the footer

    ActiveDocument.Bookmarks(strMyBMName).Select
    With Selection.Font
    .Size = 8
    End With
    End If
    Next j
    End With
    Next i
    ActiveWindow.View.Type = initial_active_window_view_type
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub

    I think the FRAMES calls in the macro were in case someone used a frame for page numbering in the footer. This was written by Greg for someone else who had that requirement, and I think some of my users may do things like that with their page numbering so I left it in.

  6. #6
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: [RESOLVED][Word 2010] Insert text in all footers in a document

    Quote Originally Posted by bflo1127 View Post
    I wonder if, in your testings, you left the footers linked to one another. I turned off all links to previous.
    That might of had a little something to do with it, I didn't do anything special with my testing doccument because I didn't know.

    Glad you got it worked out.

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
  •  



Click Here to Expand Forum to Full Width