gpt4 book ai didi

vba - 搜索所有打开的文档,查找文本,删除到文档末尾 Word宏

转载 作者:行者123 更新时间:2023-12-03 23:08:29 25 4
gpt4 key购买 nike

我正在尝试编写一个 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 = 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

最佳答案

下面的代码应该可以完成您在文档主体中查找的内容。不确定您使用 StoryRanges 的确切原因。我对那个集合不太熟悉,所以没有包含它。

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

关于vba - 搜索所有打开的文档,查找文本,删除到文档末尾 Word宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13127721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com