Word 2010 VBA - 嵌套 Do Until ... 循环搜索和替换
Word 2010 VBA - Nested Do Until ... Loop for Search and Replaces
我正在尝试创建一个嵌套的 Do Until... 循环,它将在 Word 文档中执行 15 种不同的搜索和替换。我需要完成以下工作(末尾的分号仅用于列表目的):
Search space ^t; replace with ^t;
Search ^t space; replace with ^t;
search ^t^t; replace with ^t;
search ^t^p; replace with ^p;
search $^t; replace with $;
search $ space; replace with $;
search ^t%; replace with %;
search space %; replace with %;
search (^t; replace with (;
search ( space; replace with (;
search ^t); replace with );
search space ); replace with );
search space ^p; replace with ^p;
search ^p space; replace with ^p; and
search ^p^p; replace with ^p.
我录制了宏,想编辑它,替换掉众多
Selection.Find.Execute 替换:=wdReplaceAll
对于宏中的每个 search/replace 都会发生,直到结果为 0 个实例。我知道 Do Until... Loop 会使这个宏更有效率。请帮忙!
按照这些思路至少可以让您入门:
Dim searches() As String
Dim replaces() As String
searches = Split("( , %", ",")
replaces = Split("(,%", ",")
Dim i As Integer
For i = LBound(searches) To UBound(searches)
With Selection.Find
Do
.ClearFormatting
.Text = searches(i)
.Replacement.Text = replaces(i)
.Execute Replace:=wdReplaceAll
Loop While .Found
End With
Next i
搜索字符串和替换字符串以逗号分隔,并且必须是相同数量的元素 - 为了清楚起见,只包含两个(将 vbTab 和 vbCr 到处串联起来会很丑陋)。
我正在尝试创建一个嵌套的 Do Until... 循环,它将在 Word 文档中执行 15 种不同的搜索和替换。我需要完成以下工作(末尾的分号仅用于列表目的):
Search space ^t; replace with ^t;
Search ^t space; replace with ^t;
search ^t^t; replace with ^t;
search ^t^p; replace with ^p;
search $^t; replace with $;
search $ space; replace with $;
search ^t%; replace with %;
search space %; replace with %;
search (^t; replace with (;
search ( space; replace with (;
search ^t); replace with );
search space ); replace with );
search space ^p; replace with ^p;
search ^p space; replace with ^p; and
search ^p^p; replace with ^p.
我录制了宏,想编辑它,替换掉众多 Selection.Find.Execute 替换:=wdReplaceAll 对于宏中的每个 search/replace 都会发生,直到结果为 0 个实例。我知道 Do Until... Loop 会使这个宏更有效率。请帮忙!
按照这些思路至少可以让您入门:
Dim searches() As String
Dim replaces() As String
searches = Split("( , %", ",")
replaces = Split("(,%", ",")
Dim i As Integer
For i = LBound(searches) To UBound(searches)
With Selection.Find
Do
.ClearFormatting
.Text = searches(i)
.Replacement.Text = replaces(i)
.Execute Replace:=wdReplaceAll
Loop While .Found
End With
Next i
搜索字符串和替换字符串以逗号分隔,并且必须是相同数量的元素 - 为了清楚起见,只包含两个(将 vbTab 和 vbCr 到处串联起来会很丑陋)。