简体   繁体   English

MS Word中的VBA会影响整个文档,而不仅仅是选择

[英]VBA in MS Word affects whole document, not just selection

I want to have a VBA script perform a find / replace in a selection and then assign the macro to a button on the Quick Access ToolBar, to save having to click through the usual Find/Replace procedure. 我想让VBA脚本在选择中执行查找/替换,然后将宏分配给快速访问工具栏上的按钮,以节省单击常规查找/替换过程的麻烦。

I recorded a macro while doing this and this is what I get: 我这样做时记录了一个宏,这就是我得到的:

 Sub FindReplace()
      '
      ' FindReplace Macro
      '
      '
     Selection.Find.ClearFormatting
     Selection.Find.Replacement.ClearFormatting
     With Selection.Find
         .Text = "^p"
         .Replacement.Text = " "
         .Forward = True
         .Wrap = wdFindAsk
         .Format = False
         .MatchCase = False
         .MatchWholeWord = False
         .MatchWildcards = False
         .MatchSoundsLike = False
         .MatchAllWordForms = False
     End With
     Selection.Find.Execute Replace:=wdReplaceAll
 End Sub

But when I run this macro on a selection to change paragraph marks into spaces it continues on to change the whole document, not just the selection. 但是,当我在选择项上运行此宏以将段落标记更改为空格时,它将继续更改整个文档,而不仅仅是选择项。

I'm no stranger to VBA but I can't see how to fix it so it stops when the selection is done. 我对VBA并不陌生,但是我看不到如何解决它,因此选择完成后它会停止。

You are going to need to use 您将需要使用

     .Wrap = wdFindStop

Instead of 代替

     .Wrap = wdFindAsk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM