Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
Ronald Pero Ronald Pero is offline
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.word.docmanagement
Stefan Blom[_3_] Stefan Blom[_3_] is offline
external usenet poster
 
Posts: 6,897
Default 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

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and replace macro needed GregNga Microsoft Word Help 10 February 19th 10 08:44 PM
Search and replace macro needed GregNga Microsoft Word Help 0 February 18th 10 09:13 PM
Macro for search and replace not working Brad in Tampa Microsoft Word Help 2 August 21st 07 06:01 AM
Search-and-replace macro never works Liam Gibbs Microsoft Word Help 3 May 8th 07 12:38 AM
search and replace macro genius? BobW Microsoft Word Help 1 March 3rd 06 04:40 PM


All times are GMT +1. The time now is 05:10 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"