Word VBA查找/替换问题 [英] Word VBA Find/Replace Issue

查看:515
本文介绍了Word VBA查找/替换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Word中使用VBA查找和替换,我遇到的问题是,例如,如果我尝试将所有"John(space)Smith"实例替换为"John(non-breaking space)Smith",则查找替换函数将所有"John(space)Smith"以及所有"John(non-breaking space)Smith"替换为"John(non-breaking space)Smith".在我使用它并留下曲目更改之前,我真的不希望它用它自己替换.我尝试使用Chr(32)仅选择空间,但仍然无济于事. (我只是在这里指出,我不是在字面上写(空格)和(不间断空格),而是在象征空格和不间断空格所在的位置)

Using the VBA Find and Replace in Word i have the issue that if i am trying to replace all instances of "John(space)Smith" for example, with "John(non-breaking space)Smith", the Find Replace function replaces all "John(space)Smith" as well as all "John(non-breaking space)Smith" with "John(non-breaking space)Smith". Before i am using this and leaving track changes i don't really want it to replace it with itself. I have tried using Chr(32) to only select the space but still to no avail. (I will just point out here that i am not literally writing (space) and (non-breaking space) but it is just symbolizing where the spaces and non-breaking spaces are)

在查找时,查找/替换似乎无法区分空格和不间断空格.我以相反的方向尝试了它,但是效果很好(可以在不间断的空间中找到并替换空间).所以我的理论是,对于查找/替换",将不间断的空间视为一个空间,但将一个空间不视为不间断的空间.知道这一点,任何人都可以提供解决方案来解决这个问题吗?我可以以某种方式遍历文档并找到"John(space)Smith"的实例并进行相应替换吗?循环方法应避免此问题,但我不确定如何实现.

It seems that the Find/Replace doesn't differentiate between a space and a non-breaking space when it does a find. I tried it in the opposite direction and it works fine however (non-breaking space to space find and replace). So my theory is that for a Find/Replace, a non-breaking space is regarded as a space but a space is not regarded as a non-breaking space. Knowing this, can anyone offer a solution to get past this? Can i somehow loop through a document and find instances of "John(space)Smith" and replace accordingly? A loop method should avoid this issue but i am not sure how to implement it.

ActiveDocument.Range.Select

strFind = "John" & Chr(32) & "Smith"
strReplace = "John" & Chr(202) & "Smith" 'note the chr(202) is because i am working on a mac. 160 on pc i believe

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
 .Text = strFind
 .Replacement.Text = strReplace
 .Forward = True
 .Wrap = wdFindAsk
 .Format = True
 .MatchCase = False
 .MatchWholeWord = False
 .MatchWildcards = False
 .MatchSoundsLike = False
 .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

谢谢

Cameron

推荐答案

您需要使用通配符.因此,将适当的行更改为以下行:

You need to use wildcards to do so. Therefore change appropriate lines into the following ones:

strFind = "(John)( )(Smith)"
strReplace = "\1^s\3"      'for Win, and for Mac try: "\1" & chr(202) & "\3"

并将此行设置为true:

and set this line into true:

.MatchWildcards = True

有关更多信息,请参见此链接.

For more information please see THIS LINK.

这篇关于Word VBA查找/替换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆