简单的 Word VBA 替换宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11019217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:28:51  来源:igfitidea点击:

Simple Word VBA replacement macro

vbams-word

提问by SwarthyMantooth

I'm trying to search through a Word document and replace a specific string (Using a hardcoded value of "Special" for now) with a specified value. The macro clearly shows that it finds the correct text in the Word editor, but the replacement functionality isn't working. This is my absolute first VBA experience, so I'd love some help if possible. The following code is the macro...

我正在尝试搜索 Word 文档并用指定的值替换特定的字符串(暂时使用“特殊”的硬编码值)。该宏清楚地表明它在 Word 编辑器中找到了正确的文本,但替换功能不起作用。这是我绝对的第一次 VBA 体验,所以如果可能的话,我希望得到一些帮助。下面的代码是宏...

    Sub Test()
'
' Test Macro
'
'
    Documents.Open FileName:="C:\Users\abensch\Documents\NANTDocMerge\DMID - Backups\System Clock Ability.docx", _
        ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, _
        PasswordDocument:="", PasswordTemplate:="", _
        Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", _
        Format:=wdOpenFormatAuto, _
        XMLTransform:=""
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Special"
        .Replacement.Text = "Potato"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute

End Sub

I'm not sure that most of the specified parameters in the Select.Find are necessary, and removing them doesn't seem to influence the performance of the macro, but I figured I'd leave them in to be safe. I'm wondering if it may be a Word formatting issue, although I tried to remove all formatting with the two ClearFormatting calls.

我不确定 Select.Find 中的大多数指定参数是否是必需的,删除它们似乎不会影响宏的性能,但我想我会保留它们以确保安全。我想知道这是否可能是 Word 格式问题,尽管我尝试使用两个 ClearFormatting 调用删除所有格式。

回答by Siddharth Rout

If you want to do a Replace all then change

如果你想做一个替换全部然后改变

Selection.Find.Execute

to

Selection.Find.Execute Replace:=wdReplaceAll

If you want to just replace the first instance and not all then use this

如果您只想替换第一个实例而不是全部,请使用它

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "Special"
    .Replacement.Text = "Potato"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
With Selection
    If .Find.Forward = True Then
        .Collapse Direction:=wdCollapseStart
    Else
        .Collapse Direction:=wdCollapseEnd
    End If
    .Find.Execute Replace:=wdReplaceOne
End With