VBA Word:在 Word 文档中找到的文本之前插入文本
VBA Word: Insert text before found text in word document
我想做的是一些简单的事情(也许)。我写了一个查找方法来循环文档。我有查找值 - ']' 和插入值 - '[('。在这两者之间可能有任何类型的数字,最有可能是 1 - 100。
我目前的代码是这样的:
Sub FindBracket()
Dim FindValue As String
Dim ReplaceValue As String
Dim oRange As Range
Set oRange = ActiveDocument.Range
FindValue = "]"
InsertValue = "(["
With oRange.Find
.Text = FindValue
.MatchWholeWord = False
Do While .Execute = True
If .Found Then
oRange.InsertBefore (InsertValue)
End If
oRange.Start = oRange.End
oRange.End = ActiveDocument.Range.End
Loop
End With
End Sub
find方法很好,但我需要修改.InsertBefore方法。
例子
我需要将 ' Dummy text 1] ' 转换为 ' Dummy text [(1] ' 但从代码中我得到 ' 虚拟文本 1[(] ',我似乎不知道该怎么做,而且我之前只有 Excel VBA 的经验。
非常感谢您对此的任何帮助。
试试这个:
Sub FindBracket()
Dim FindValue As String
Dim ReplaceValue As String
Dim oRange As Range
Set oRange = ActiveDocument.Content
FindValue = "[0-9]@\]"
InsertValue = "[(^&" ' ^& - found number
With oRange.Find
.Text = FindValue
.Replacement.Text = InsertValue
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True ' enabled regular expressions
.Execute Replace:=wdReplaceAll
End With
End Sub
Before:
Dummy text 1234]
Dummy text 4321]
Dummy text 0]
Dummy text 1]
After:
Dummy text [(1234]
Dummy text [(4321]
Dummy text [(0]
Dummy text [(1]
您只需要:
Sub FindBracket()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[0-9]@\]"
.Replacement.Text = "[(^&"
.Forward = True
.Format = False
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
事实上,您甚至不需要宏 - 这一切都可以通过 GUI 使用 通配符 Find/Replace 和相同的 F/R表达式。
我想做的是一些简单的事情(也许)。我写了一个查找方法来循环文档。我有查找值 - ']' 和插入值 - '[('。在这两者之间可能有任何类型的数字,最有可能是 1 - 100。
我目前的代码是这样的:
Sub FindBracket()
Dim FindValue As String
Dim ReplaceValue As String
Dim oRange As Range
Set oRange = ActiveDocument.Range
FindValue = "]"
InsertValue = "(["
With oRange.Find
.Text = FindValue
.MatchWholeWord = False
Do While .Execute = True
If .Found Then
oRange.InsertBefore (InsertValue)
End If
oRange.Start = oRange.End
oRange.End = ActiveDocument.Range.End
Loop
End With
End Sub
find方法很好,但我需要修改.InsertBefore方法。
例子 我需要将 ' Dummy text 1] ' 转换为 ' Dummy text [(1] ' 但从代码中我得到 ' 虚拟文本 1[(] ',我似乎不知道该怎么做,而且我之前只有 Excel VBA 的经验。
非常感谢您对此的任何帮助。
试试这个:
Sub FindBracket()
Dim FindValue As String
Dim ReplaceValue As String
Dim oRange As Range
Set oRange = ActiveDocument.Content
FindValue = "[0-9]@\]"
InsertValue = "[(^&" ' ^& - found number
With oRange.Find
.Text = FindValue
.Replacement.Text = InsertValue
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True ' enabled regular expressions
.Execute Replace:=wdReplaceAll
End With
End Sub
Before:
Dummy text 1234]
Dummy text 4321]
Dummy text 0]
Dummy text 1]
After:
Dummy text [(1234]
Dummy text [(4321]
Dummy text [(0]
Dummy text [(1]
您只需要:
Sub FindBracket()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[0-9]@\]"
.Replacement.Text = "[(^&"
.Forward = True
.Format = False
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
事实上,您甚至不需要宏 - 这一切都可以通过 GUI 使用 通配符 Find/Replace 和相同的 F/R表达式。