vba word: If Then 查找并替换
vba word: If Then find and replace
在此代码中,我想执行以下操作:
- 如果单词结尾是字母M,替换成字母N。
- 如果单词以字母 N 结尾,则替换为字母 M。
我不太了解如何使用 IF - Then 语句。
任何帮助将不胜感激。
Sub find_end()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[nm]>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
Selection.Find.Execute
With Selection
If Selection.Find.Found = n Then
Selection.TypeText Text:=m
ElseIf Selection.Find.Found = m Then
Selection.TypeText Text:=n
End If
End Sub
我修改了在以下位置找到的代码:Repeating Microsoft Word VBA until no search results found
它将遍历文档并替换每个单词的最后一个字母(如果它是 'm' 或 'n')。请注意,如果您可能会发现超过 2000 m 或 n's,您可能希望删除该代码中的“循环检查”。
Option Explicit
' The following code adapted from:
Sub SearchFN()
Dim iCount As Integer
Dim lStart As Long
'Always start at the top of the document
Selection.HomeKey Unit:=wdStory
'find a footnote to kick it off
With Selection.Find
.ClearFormatting
.Text = "[nm]>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
.Execute
End With
'Jump back to the start of the document.
Selection.HomeKey Unit:=wdStory
'If we find one then we can set off a loop to keep checking
'I always put a counter in to avoid endless loops for one reason or another
Do While Selection.Find.Found = True And iCount < 2000
iCount = iCount + 1
Selection.Find.Execute
'On the last loop you'll not find a result so check here
If Selection.Find.Found Then
' Exit if we start back at the beginning
If Selection.Start < lStart Then
Exit Do
End If
'Reset the find parameters
With Selection.Find
.ClearFormatting
.Text = "[nm]>"
If Selection.Text = "m" Then
Debug.Print "found 'm' at position: " & Selection.Start
Selection.Text = "n"
ElseIf Selection.Text = "n" Then
Debug.Print "found 'n' at position: " & Selection.Start
Selection.Text = "m"
End If
lStart = Selection.Start
' .Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
End If
Loop
End Sub
在此代码中,我想执行以下操作:
- 如果单词结尾是字母M,替换成字母N。
- 如果单词以字母 N 结尾,则替换为字母 M。
我不太了解如何使用 IF - Then 语句。 任何帮助将不胜感激。
Sub find_end() Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "[nm]>" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .MatchWildcards = True Selection.Find.Execute With Selection If Selection.Find.Found = n Then Selection.TypeText Text:=m ElseIf Selection.Find.Found = m Then Selection.TypeText Text:=n End If End Sub
我修改了在以下位置找到的代码:Repeating Microsoft Word VBA until no search results found 它将遍历文档并替换每个单词的最后一个字母(如果它是 'm' 或 'n')。请注意,如果您可能会发现超过 2000 m 或 n's,您可能希望删除该代码中的“循环检查”。
Option Explicit
' The following code adapted from:
Sub SearchFN()
Dim iCount As Integer
Dim lStart As Long
'Always start at the top of the document
Selection.HomeKey Unit:=wdStory
'find a footnote to kick it off
With Selection.Find
.ClearFormatting
.Text = "[nm]>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
.Execute
End With
'Jump back to the start of the document.
Selection.HomeKey Unit:=wdStory
'If we find one then we can set off a loop to keep checking
'I always put a counter in to avoid endless loops for one reason or another
Do While Selection.Find.Found = True And iCount < 2000
iCount = iCount + 1
Selection.Find.Execute
'On the last loop you'll not find a result so check here
If Selection.Find.Found Then
' Exit if we start back at the beginning
If Selection.Start < lStart Then
Exit Do
End If
'Reset the find parameters
With Selection.Find
.ClearFormatting
.Text = "[nm]>"
If Selection.Text = "m" Then
Debug.Print "found 'm' at position: " & Selection.Start
Selection.Text = "n"
ElseIf Selection.Text = "n" Then
Debug.Print "found 'n' at position: " & Selection.Start
Selection.Text = "m"
End If
lStart = Selection.Start
' .Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
End If
Loop
End Sub