在VBA中,我有一个文档,下面有这个文本,我需要一个vba宏来循环它并删除
“ | unwanted_text ”
从各行,有人可以帮我吗?
example1 | unwanted_text | example2 | example3 |例4
你好世界
example1 | unwanted_text | example2 | example3 |例4
你好世界
最终结果必须是这样的:
example1 | example2 | example3 |例4
你好世界
example1 | example2 | example3 |例4
你好世界
注意: unwanted_text 可以是任何变量字符串
答案 0 :(得分:0)
以下代码将: (a)通过你的文件旋转 (b)确定是否找到该模式 (c)如果找到模式,删除第2和第3个单词。
Sub Delete_Words()
Dim oPara As Word.Paragraph
On Error GoTo Error_Trap
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Words.Count > 3 Then
' You can check for specific values of word 2 and 4
' Then could delete whatever is between
'Debug.Print oPara.Range.Words(2).Text & " <> " & oPara.Range.Words(4).Text
If Trim(oPara.Range.Words(2).Text) = "|" And Trim(oPara.Range.Words(4).Text) = "|" Then
' NOTE!! Must delete word 3 before word 2, else incorrect results!
oPara.Range.Words(3).Delete
oPara.Range.Words(2).Delete
End If
Else
' Not enough words
End If
Next
Exit Sub
Error_Trap:
Debug.Print "Error: " ^ Err.Number & vbTab & Err.Description
MsgBox "Error: " ^ Err.Number & vbTab & Err.Description
End Sub