PDA

View Full Version : how to use Word find and Replace to search inside a string



peroron2000
09-28-2013, 05:02 PM
I am trying to figure out how to use the power of Word's Find and Replace to search inside of string variables and then replace the found sequence with something else or in some cases to remove the found item from the variable. I assumed that the following example code would work, in this case I thought it would find 5 numerical digits inside the string variable sSample and remove them but it doesn't seem to work

Sub Macro2()
'
' Macro2 Macro
'
'
sSample = "aaaaaa43215aaaaaaaa"
sSampleReplaceString = "[0-9]{5}"


Selection.Find.Replacement.ClearFormatting
With sSample.Find
.Text = sSampleReplaceString
.Replacement.Text = ""
.MatchWildcards = True
End With
Selection.Find.Execute


End Sub

Can anyone tell me if what I am trying to do is possible. I would prefer to not have to first have to add the variable to the document and then find it separately and replace it from there. Thank you for your help.

Jay Freedman
09-28-2013, 05:23 PM
What you're trying to do is not impossible, but the Find object is completely the wrong tool because it operates only on a Range or Selection in the document.

The following VBA functions are useful for manipulating String variables:

InStr() -- returns the position of a substring within the larger string, if it occurs (or zero otherwise); optionally include a starting position
Replace() -- replace any occurrence of a substring with another substring
Left(), Mid(), Right() -- return part of the larger string, starting at a particular position and continuing for a given length

Another alternative is to set a reference (in Tools > References) to the Microsoft VBScript Regular Expressions 5.5 library and use its functions, which are considerably more powerful and flexible than VBA's (http://support.microsoft.com/kb/818802).

peroron2000
09-28-2013, 05:41 PM
Thanks for responding to my post. It sounds like what you are saying is that what I'm trying to do is impossible because I need to use find and replace in word to search inside of a string variable. I can't use the replace function in VBA because I need to use wildcards which replace doesn't support and I don't want to search the entire document, just the variable.

Jay Freedman
09-28-2013, 08:06 PM
Then use the regular expression functions (sometimes called 'regex'), which does support wildcards in a string search/replace.

fumei
09-28-2013, 11:56 PM
May I ask you to explain what you mean by "variable"? If you are talking about a piece of actual text in the document, then this is not a variable. You may of course assign it to a variable.

Variables only live in code.

in this case I thought it would find 5 numerical digits inside the string variable sSample and remove them but it doesn't seem to work


No, why would it? sSampleReplaceString = "[0-9]{5}" is a string, nothing more. A straight forward string does not support wildcards.
As Jay mentions, you need to ty REGEX. I also suspect you are not using Option Explicit. I strongly suggest you start using it.


With sSample.Find as coded is not valid.