vba Word 2003 modify headers

  • Thread starter Thread starter Tith
  • Start date Start date
T

Tith

I have a macro to run through the document and change all my red text to
blue, but it doesn't affect my headers. How do I get it to incorporate my
headers as well?

Public Sub Red2Blue()
With ActiveDocument.Content.Find
.ClearFormatting
.Font.Color = wdColorRed
With .Replacement
.ClearFormatting
.Font.Color = wdColorBlue
End With

.Execute FindText:="", ReplaceWith:="", Format:=True,
Replace:=wdReplaceAll
End With
End Sub
 
Unlike the UI Find and Replace operations, your code is only processing the
main text storyrange of the document. At the very least you would need to
interate through the three different header storyranges. This still
wouldn't pick up text in the footers, textboxes, comments, and other
storyranges.

See: http://gregmaxey.mvps.org/VBA_Find_And_Replace.htm
 
Public Sub Red2Blue()
' Replace the headers Text from Red to Blue

Set MyStoryRange = ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
Do Until MyStoryRange Is Nothing

With MyStoryRange.Find
.ClearFormatting
.Font.Color = wdColorRed
With .Replacement
.ClearFormatting
.Font.Color = wdColorBlue
End With

.Execute FindText:="", ReplaceWith:="", Format:=True,
Replace:=wdReplaceAll
End With

Set MyStoryRange = MyStoryRange.NextStoryRange
Loop

' Replace the body Text from Red to Blue
With ActiveDocument.Content.Find
.ClearFormatting
.Font.Color = wdColorRed
With .Replacement
.ClearFormatting
.Font.Color = wdColorBlue
End With

.Execute FindText:="", ReplaceWith:="", Format:=True,
Replace:=wdReplaceAll
End With
End Sub
 
Back
Top