【发布时间】:2019-01-30 01:56:33
【问题描述】:
我已经手动完成了此操作,我想用从 1 开始一直到 ZXZ 在 word 文档中最后一次出现的序列号替换每个 ZXZ 实例。
基本上,我想要实现的是在特定的 word 文档中运行 word VBA 宏,其中 VBA 宏从文件顶部开始搜索,查找 ZXZ 的每个出现,将第一次出现的 ZXZ 替换为“1”,然后是“2”的下一个出现顺序,直到在 word 文档中找到最后一个 ZXZ。
示例 word 文档可能包含:
元素 ZXZ 元素ZXZ ... 元素ZXZ
运行 vba word 宏后,我想得到:
元素 1 元素 2 ... 元素 25
我已经使用此代码完成了此操作,但我想执行“while”循环或查找 ZXZ 的每次出现并将其替换为从“1”开始的序号
Sub my_prov_MDList()
'
' my_prov_MDList Macro
'
'
Selection.MoveUp Unit:=wdScreen, Count:=7
Selection.HomeKey Unit:=wdLine
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "ZXZ"
.Replacement.Text = "1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
With Selection.Find
.Text = "ZXZ"
.Replacement.Text = "2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
''''我基本上一直重复上面的代码,直到我达到 25,这通常是每个文档中存在的 ZXZ 实例的数量。
【问题讨论】: