Replace word of specific length

J

Jarryd

Hi,

I have a document that I would like to convert to a cxv file. But before I
do there are some strings that I would like removed. They are all 5
characters in length, comprised of only numeric characters (0-9) and begin
with the number 7. Does anyone have any examples of VBA code that does this
sort of thing?

TIA,

Jarryd
 
J

Jonathan West

Jarryd said:
Hi,

I have a document that I would like to convert to a cxv file. But before
I do there are some strings that I would like removed. They are all 5
characters in length, comprised of only numeric characters (0-9) and begin
with the number 7. Does anyone have any examples of VBA code that does
this sort of thing?

With ActiveDocument.Range.Find
.Format = False
.Text = "7^#^#^#^#"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg

You should need VBA. Edit>Replace>More Use Wildcards

Type 7[0-9]{4}"add a trailing space"

in the find what and leave the replace with empty.

Click Replace All.

The trailing space prevents you from finding patterns with more than
five numbers.
 
K

Klaus Linke

If you want to avoid deleting longer strings containing a "7", use
".MatchWildcards = True" and ".Text = "<7[0-9]{4}>".

You might still match some decimal numbers like 71234.56 -- if that could be
a problem, you'd need a more complicated wildcard replacement.

Say,
.Text = "<7[0-9]{4}>([!,.\-])"
.Replacement.Text = "\1"

If you know there's a space in front or after the number, you could include
that in the Find expression, else you could replace two spaces with one
afterwards to clean them up.

Greetings,
Klaus
 
J

Jarryd

Hi Jonathan and Klaus. All sorted now, thanks to you.

Jarryd
Klaus Linke said:
If you want to avoid deleting longer strings containing a "7", use
".MatchWildcards = True" and ".Text = "<7[0-9]{4}>".

You might still match some decimal numbers like 71234.56 -- if that could
be a problem, you'd need a more complicated wildcard replacement.

Say,
.Text = "<7[0-9]{4}>([!,.\-])"
.Replacement.Text = "\1"

If you know there's a space in front or after the number, you could
include that in the Find expression, else you could replace two spaces
with one afterwards to clean them up.

Greetings,
Klaus


Jonathan West said:
With ActiveDocument.Range.Find
.Format = False
.Text = "7^#^#^#^#"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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