Find and Replace with VBA

K

kheisler6

I'm trying to FIND every instance of a period (".") and REPLACE it with
a period and a space (". "). The code I've come up with works. How do I
edit the code so that it ignores the operation when it finds a period
followed immediately by a comma (".,"). I would like such instances to
be remain untouched.

Thanks. - Kurt

###

' AddSpace Macro
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "."
.Replacement.Text = ". "
.Forward = True
.Wrap = wdFindContinue
End With

If Selection.Find.Execute Then
Selection.Find.Execute Replace:=wdReplaceAll
End If
 
D

Doug Robbins - Word MVP

Use a Wildcard replace with .[!,] as the text that is being sought.

See the article "Finding and replacing characters using wildcards" at:

http://www.word.mvps.org/FAQs/General/UsingWildcards.htm

You don't really need a macro to do this sort of thing.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

Charlie Mac

Kurt,

Consider this code fragment:

Dim AftrerChar as string
Do While Selection.Find.Execute(FindText:= ".", Forward:=True,
Format:=True, Wrap:=wdFindStop) = True
AfterChar = ActiveDocument.Range(Selection.End, Selection.End
+ 1).Text
If AfterChar = "," Then
' ignore
Else
' replace period with period and space
End If
Loop

Take care,

Charlie from Texas
 
K

kheisler6

I'm new to VBA in Word so I wasn't sure where to insert your code
fragment. In the way I've done it, the code finds every "." and deletes
it, so I obviously did something wrong. (See code below.)

But it seems the simplest way would be to find the correct wildcard
syntax and use it in the .Text = line.

The syntax ".[!.,]" comes closest, but this finds every period AND
anything other than a comma which follows it (a letter, a space, etc.).
So it skips over ".," but finds ".L", ". ", etc.

Ideas? Thanks.
 
K

kheisler6

I'm new to VBA in Word so I wasn't sure where to insert your code
fragment. In the way I've done it, the code finds every "." and deletes
it, so I obviously did something wrong. (See code below.)

But it seems the simplest way would be to find the correct wildcard
syntax and use it in the .Text = line.

The syntax ".[!.,]" comes closest, but this finds every period AND
anything other than a comma which follows it (a letter, a space, etc.).
So it skips over ".," but finds ".L", ". ", etc.

Ideas? Thanks.

###

Dim AftrerChar As String
Do While Selection.Find.Execute(FindText:=".", _
Forward:=True, Format:=True, Wrap:=wdFindStop) = True & _
AfterChar = ActiveDocument.Range(Selection.End, Selection.End +
1).Text
If AfterChar = "," Then
'Ignore
Else
'Replace period with period and space
With Selection.Find
.Text = "."
.Replacement.Text = ". "
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End If

Loop

If Selection.Find.Execute Then
Selection.Find.Execute Replace:=wdReplaceAll
End If
 
G

Greg Maxey

Try this:
Sub Test1()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".[!,]"
.MatchWildcards = True
.Replacement.Text = ". "
.Forward = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 

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