怎样用VB代码实现查找替换word页眉里的文字?
比如页眉里面有“行程”的文字全部替换成“预定”,用vb代码如何实现?
怎样用VB代码实现查找替换word页眉里的文字?
答案:2 悬赏:70 手机版
解决时间 2021-01-26 06:53
- 提问者网友:却不属于对方
- 2021-01-25 17:57
最佳答案
- 五星知识达人网友:孤老序
- 2021-01-25 18:58
参考这个代码替换页眉里的文字(注意这个代码只适用于奇偶页眉相同的情形):
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
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
全部回答
- 1楼网友:拜訪者
- 2021-01-25 20:34
with worddoc.activewindow if .view.splitspecial <> wdpanenone then .panes(2).close end if if .activepane.view.type = wdnormalview or .activepane.view.type = wdoutlineview or .activepane.view.type = wdmasterview then .activepane.view.type = wdpageview end if .activepane.view.seekview = wdseekcurrentpageheader end with with wordsel .font.name = "楷体_gb2312" .font.bold = true .paragraphformat.alignment = wdalignparagraphright .typetext text:="这里是页眉!" '这里是logo图片 dim logofile as string logofile = path.app & "logo.bmp" on error resume next kill logofile on error goto 0 savepicture imgmylogo.picture, logofile wordapp.activedocument.shapes.addpicture anchor:=.range, filename:=logofile, linktofile:=false, savewithdocument:=true kill logofile .movedown unit:=wdline, count:=1 .font.bold = true .font.name = "楷体_gb2312" .paragraphformat.alignment = wdalignparagraphcenter .typetext text:="这里是页脚" worddoc.activewindow.activepane.view.seekview = wdseekmaindocument end with
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯