VBA 宏,用于查找字符串文档中的所有实例,例如“一只狗”,并随后立即插入跟踪的参考数字,例如“(102)”

问题描述 投票:0回答:1

我希望在 Word 中使用 VBA 宏来查找文档中用户选择的字符串的所有实例(例如“狗”),并在括号中插入相应的用户选择的参考数字(例如“(102)” ) 紧跟在每个字符串之后。

我制作了一个查找和替换样式宏,但是在跟踪的更改中,它显示为人为修订,删除和重新插入字符串本身,而我只希望插入的参考数字在跟踪中显示为修订.

我真的很感激关于如何仅显示跟踪中的参考数字的任何想法,例如,通过随后添加循环以接受特定的字符串删除和重新插入,或者使用完全不同的方法。我被难住了。

这是我不充分的查找和替换代码,它成功地提供了跟踪的参考数字,但这样做是以删除并重新插入字符串本身为代价的(这不是我想要的,因为这显示为人工制品修订)在Word文档中):

Sub RefNumerals()

Selection.Find.ClearFormatting
feature = InputBox("Type in claim feature:")
refnum = InputBox("Type in reference numeral")
With Selection.Find
.Text = feature
.Replacement.Text = feature & " (" & refnum & ")"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With

Selection.Find.Execute Replace:=wdReplaceAll
End Sub
vba replace ms-word
1个回答
0
投票
  • 使用
    InserAfter
  • 插入参考编号
Sub RefNumerals()
    Dim feature, refnum
    feature = InputBox("Type in claim feature:")
    refnum = InputBox("Type in reference numeral")
    ' feature = "dog" ' for testing
    ' refnum = "100"
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Text = feature
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        Do While .Execute
            .Parent.InsertAfter " (" & refnum & ")"
            .Parent.Collapse wdCollapseEnd
        Loop
    End With
End Sub

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.