[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.
Re: [Word 2010] Insert text in all footers in a document
Why are you running 2 loops? Just Try:
vb Code:
Public Sub FooterTest()
Dim oSection As Section
For Each oSection In ActiveDocument.Sections
FooterEdit
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 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.
Re: [Word 2010] Insert text in all footers in a document
Thanks! I'll give that a try and let you know!
Re: [Word 2010] Insert text in all footers in a document
try like
vb Code:
with thisdocument.sections(1)
for i = 1 to 3
.footers(i).range.text = .footers(i).range.text & vbnewline & "some more footer"
next
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)
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.
Re: [RESOLVED][Word 2010] Insert text in all footers in a document
Quote:
Originally Posted by
bflo1127
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.