怎样用VB代码实现查找替换word页眉里的文字?
怎样用VB代码实现查找替换word页眉里的文字?
比如页眉里面有“行程”的文字全部替换成“预定”,用vb代码如何实现?
参考这个代码替换桥此岩页眉里的文字(注意这个代码只适用于奇偶页眉相同的情形):扒饥
Sub ReplaceAllInHeader(strFind As String, strReplace As String)
Dim oStoryRange As Range
Set oStoryRange = ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
oStoryRange.Find.ClearFormatting
oStoryRange.Find.Replacement.ClearFormatting
With oStoryRange.Find
.Text = strFind
.Replacement.Text = strReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oStoryRange.Find.Execute Replace:=wdReplaceAll
End Sub
如果想针对全文进行替换(包括正文、普通页眉、奇偶页眉、普通页码
、奇偶页码敏御、脚注、尾注等等):
Sub ReplaceAll(strFind As String, strReplace As String)
Dim oStoryRange As Range
For Each oStoryRange In ActiveDocument.StoryRanges
oStoryRange.Find.ClearFormatting
oStoryRange.Find.Replacement.ClearFormatting
With oStoryRange.Find
.Text = strFind
.Replacement.Text = strReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oStoryRange.Find.Execute Replace:=wdReplaceAll
Next
End Sub