simplifying find and replace

J

jason gefre

i'm pretty new to VBA, with most of my experience being with record macros, then tweaking a bit. I made a large macro to re-format work related items, but the find and replace ends up running through the document hundreds of times, and i was hoping there was a way to simplify the process. I was told of 'streaming' before, but i dont know how i'd implement this.

here's some examples of the code; i actually have around 100 different replacements with either a simple or wild search and a replacement

'space fixer
Call SReplaceAll(" ", " ")
Call SReplaceAll(" {2,}", " ")
'Line Fix
Call WReplaceAll("[!A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\-\=\_\+\`\~\[\]\{\}\;\'\:\""\,\.\<\>\/\?\\\|
?????]", "?")
Call SReplaceAll("^l", "?")
Call WReplaceAll(" @", " ")
Call SReplaceAll(" ?", "?")
Call SReplaceAll("? ", "?")
'find average fields
'call bolder (?([A-Za-z ]{3,9}:)")
Call WReplaceAll("?([A-Z {3,9}:)", "??\1")
'kill any to/from text
Call WReplaceAll("?*?", "??")
Call WReplaceAll("[!?])??HTTP:", "\1 HTTP:")
Call SReplaceAll("?", "?")
'call sreplaceall("?","^l")
'remove page breaks
Call WReplaceAll("?{1,4}PAGE*\)", "")
'Bullets
'0.A
Call WReplaceAll("?([0-9]{1,2}).([!0-9])", "??\1.\2")
'A.1
Call WReplaceAll("?([A-Z]{1}).([!0-9])", "??\1.\2")
'(a)
'call wreplaceall("?\(([A-Z0-9]{1,2})\)","??(\1)")
' -
Call WReplaceAll("?([\- ]{1,3})", "??\1")



the subs called:

Public Sub SReplaceAll(ReplaceIn, ReplaceOut)
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ReplaceIn
.Replacement.Text = ReplaceOut
.Wrap = wdFindContinue
.MatchWildcards = False
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Public Sub WReplaceAll(ReplaceIn, ReplaceOut)
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ReplaceIn
.Replacement.Text = ReplaceOut
.Wrap = wdfindcontinue
.MatchWildcards = False
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF Reflection Effect
http://www.eggheadcafe.com/tutorial...-beab-49bd76e20b9b/wpf-reflection-effect.aspx
 
M

macropod

Hi Jason,

What do you mean by 'streaming'?

FWIW, at least some of the expressions seem redundant or invalid. For example:
Call SReplaceAll(" ", " ") seems to do nothing useful - it simply replaces space characters on a one-for one basis.
and:
Call WReplaceAll("[!A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\-\=\_\+\`\~\[\]\{\}\;\'\:\""\,\.\<\>\/\?\\\|?????]", "?") seems to have an invalid
find expression that seems to be looking for paragraph marks and/or linefeeds and replacing them with '?' characters.

It's hard to know though, as they appear to be wildcard expressions but both called subs (which are identical except for the
'.MatchCase = False' in one and '.Forward = True' in the other) have '.MatchWildcards = False'.

It's quite possible that some of the expressions could be combined, deleted and/or simplified but, without knowing exactly what
you're trying to achieve, it's impossible to know for sure - especially when we can see only part of the code and none of the
before/after data.

--
Cheers
macropod
[Microsoft MVP - Word]


i'm pretty new to VBA, with most of my experience being with record macros, then tweaking a bit. I made a large macro to
re-format work related items, but the find and replace ends up running through the document hundreds of times, and i was hoping
there was a way to simplify the process. I was told of 'streaming' before, but i dont know how i'd implement this.

here's some examples of the code; i actually have around 100 different replacements with either a simple or wild search and a
replacement

'space fixer
Call SReplaceAll(" ", " ")
Call SReplaceAll(" {2,}", " ")
'Line Fix
Call WReplaceAll("[!A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\-\=\_\+\`\~\[\]\{\}\;\'\:\""\,\.\<\>\/\?\\\|
?????]", "?")
Call SReplaceAll("^l", "?")
Call WReplaceAll(" @", " ")
Call SReplaceAll(" ?", "?")
Call SReplaceAll("? ", "?")
'find average fields
'call bolder (?([A-Za-z ]{3,9}:)")
Call WReplaceAll("?([A-Z {3,9}:)", "??\1")
'kill any to/from text
Call WReplaceAll("?*?", "??")
Call WReplaceAll("[!?])??HTTP:", "\1 HTTP:")
Call SReplaceAll("?", "?")
'call sreplaceall("?","^l")
'remove page breaks
Call WReplaceAll("?{1,4}PAGE*\)", "")
'Bullets
'0.A
Call WReplaceAll("?([0-9]{1,2}).([!0-9])", "??\1.\2")
'A.1
Call WReplaceAll("?([A-Z]{1}).([!0-9])", "??\1.\2")
'(a)
'call wreplaceall("?\(([A-Z0-9]{1,2})\)","??(\1)")
' -
Call WReplaceAll("?([\- ]{1,3})", "??\1")



the subs called:

Public Sub SReplaceAll(ReplaceIn, ReplaceOut)
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ReplaceIn
.Replacement.Text = ReplaceOut
.Wrap = wdFindContinue
.MatchWildcards = False
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Public Sub WReplaceAll(ReplaceIn, ReplaceOut)
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ReplaceIn
.Replacement.Text = ReplaceOut
.Wrap = wdfindcontinue
.MatchWildcards = False
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF Reflection Effect
http://www.eggheadcafe.com/tutorial...-beab-49bd76e20b9b/wpf-reflection-effect.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top