Removing specific characters when preceded by one or more spaces

S

snsd

Hi: I want to remove the forward slash from a document if and only if it is
preceded by one or more spaces. i.e. “abc/†is OK but “ /†or “ /†or “
/abcâ€â€¦ is not. The following code successfully removes the forward slash
character that is preceded by a single space from a document and replaces it
with a highlighted space. So if there are 2 or more…spaces before the forward
slash, I want it to be removed and replaced with a highlighted space. (I am
fine with all the spaces being removed and simply replacing the entire string
with a single space.) What do I need to do to modify the code to accomplish
such? Any help is greatly appreciated.

'Removes / proceeded by a single space and replaces with a highlighted space
Dim rDcmForwardSlash As Range
Set rDcmForwardSlash = ActiveDocument.Range

With rDcmForwardSlash.Find
.Text = " /"
.Replacement.Highlight = True
.Replacement.Text = " "
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
 
H

Helmut Weber

Hi snsd,

there are many ways to do that.
The following is only the way I am used to:

Sub Test001()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = " {2,}/"
.MatchWildcards = True
While .Execute
rDcm.Text = " /"
rDcm.Characters(1).HighlightColorIndex = wdYellow
rDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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