Vba 搜索所有打开的文档,查找文本,并删除到文档Word宏的末尾
我正在尝试编写一个vba word宏,用于搜索所有打开的文档,查找所有出现的文本“DocumentEnd9999”,并删除每个文档中该文本下方的所有内容Vba 搜索所有打开的文档,查找文本,并删除到文档Word宏的末尾,vba,loops,ms-word,documents,Vba,Loops,Ms Word,Documents,我正在尝试编写一个vba word宏,用于搜索所有打开的文档,查找所有出现的文本“DocumentEnd9999”,并删除每个文档中该文本下方的所有内容 Sub deletion() Dim endTerm As String endTerm = "DocumentEnd9999" 'Loop Dim Dim n, c As Integer n = Application.Documents.Count For c = 1 To n Set myRange = Applicatio
Sub deletion()
Dim endTerm As String
endTerm = "DocumentEnd9999"
'Loop Dim
Dim n, c As Integer
n = Application.Documents.Count
For c = 1 To n
Set myRange = Application.Documents(c).StoryRanges
For Each myRange In ActiveDocument.StoryRanges
Selection.Find.ClearFormatting
With Selection.Find
.Text = endTerm
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With myRange.Find
myRange.Characters.Last.Select
.Forward = True
.Wrap = wdFindAsk
End With
Application.DisplayAlerts = False
Selection.Find.Execute
Selection.Delete
Next myRange
Next c
End Sub
下面的代码应该完成您在文档主体中查找的内容。不确定您使用StoryRange的确切原因。我对那个收藏不太熟悉,所以没有包括在内
Sub deletion()
Dim endTerm As String
endTerm = "DocumentEnd9999"
Dim n, c As Integer
n = Application.Documents.Count
c = 1
Dim r As Range
Windows(c).Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = endTerm
.Forward = True
.Wrap = wdFindContinue
End With
Do
Selection.Find.Execute
Set r = ActiveDocument.Range(Selection.Range.Start, ActiveDocument.Content.End)
r.Delete
c = c + 1
On Error Resume Next
Windows(c).Activate
Loop Until c > n
End Sub
工作完美。我使用故事范围是因为我不知道自己在做什么。谢谢@用户1783736,太好了!很高兴它对你有用。谢谢你的反馈。