Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
how can I use Word find and Replace to search inside a stringvariable (in a macro)
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. |
#2
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
how can I use Word find and Replace to search inside a string variable (in a macro)
String manipulation is limited in VBA. For the best assistance with this,
let me suggest that you post in the programming forum at Microsoft Community (http://answers.microsoft.com/en-us/o...=1380482147812) or (perhaps) in the Word Developers forum at MSDN (http://social.msdn.microsoft.com/For...forum=worddev). -- Stefan Blom Microsoft Word MVP "Ronald Pero" wrote in message ... 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. |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Search and replace macro needed | Microsoft Word Help | |||
Search and replace macro needed | Microsoft Word Help | |||
Macro for search and replace not working | Microsoft Word Help | |||
Search-and-replace macro never works | Microsoft Word Help | |||
search and replace macro genius? | Microsoft Word Help |