怎样用VB代码实现查找替换word页眉里的文字

如题所述

参考这个代码替换页眉里的文字(注意这个代码只适用于奇偶页眉相同的情形):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:=wdReplaceAllEnd 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
温馨提示:答案为网友推荐,仅供参考
相似回答