gpt4 book ai didi

vba - 选择要应用的查找和替换宏的文本范围

转载 作者:行者123 更新时间:2023-12-04 07:59:01 25 4
gpt4 key购买 nike

我有一个 Word 宏,可以执行数百次查找和替换操作,但目前它将操作应用于整个文档。我需要它仅适用于“抽象”(粗体,大小写)和“引用”(粗体,大小写)之间的文本。
当前代码将更改应用于整个文档,然后在宏的末尾,它使用以下代码追溯拒绝对引用的任何更改:

With Selection.Find
.ClearFormatting
.Font.Bold = True
.MatchCase = True
.Forward = True
.Execute FindText:="References"

If .Found = True Then

Selection.Find.Execute
Selection.Collapse wdCollapseStart

Dim r1 As Range
Set r1 = Selection.Range

Selection.Find.Text = "DummyText"

Selection.WholeStory
Selection.Collapse wdCollapseEnd

Dim r2 As Range
Set r2 = ActiveDocument.Range(r1.start, Selection.start)
r2.Select

If Selection.Range.Revisions.Count >= 1 Then _
Selection.Range.Revisions.RejectAll

End If

End With
这将选择粗体“References”和“DummyText”之间的文本,这只是一些保证找不到的文本,因此它选择到文档的末尾,然后拒绝该选择中的任何更改。
我已经尝试调整它并将其放在宏的开头,以便所有查找和替换操作仅适用于 Abstract 和 References 之间的选择,如下所示:
    Selection.Find.ClearFormatting
With Selection.Find
.Text = "Abstract"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Bold = True
.MatchCase = True
.MatchWholeWord = True
End With
Selection.Find.Execute
Selection.Collapse wdCollapseStart

Dim r1 As Range
Set r1 = Selection.Range

Selection.Find.Text = "References"
Dim r2 As Range
Set r2 = ActiveDocument.Range(r1.start, Selection.start)
r2.Select

' Move cursor to start, turn on tracked changes

Selection.HomeKey Unit:=wdStory
ActiveDocument.TrackRevisions = True
With ActiveWindow.View.RevisionsFilter
.markup = wdRevisionsMarkupSimple
.View = wdRevisionsViewFinal
End With

' start replacements (these go on for ages, two examples here)

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Also "
.Replacement.Text = "Additionally, "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Therefore "
.Replacement.Text = "Therefore, "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

' and so on...


我读过的其他主题似乎建议
.Wrap = wdFindStop
在替换字段中会做我想要的,但这不起作用。
有人可以帮忙吗?干杯。

最佳答案

例如:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Abstract"
.Font.Bold = True
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Do While .Find.Execute
Set Rng = .Duplicate
With .Duplicate
.End = ActiveDocument.Range.End
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "References"
.Font.Bold = True
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
If .Find.Found = True Then
Rng.End = .Duplicate.End
Rng.Revisions.RejectAll
End If
End With
Loop
End With
Application.ScreenUpdating = True
End Sub
如果需要,上面的代码可容纳多个“抽象”和“引用”块。

关于vba - 选择要应用的查找和替换宏的文本范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66553257/

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