Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

Replace word of specific length

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jarryd - 23 Mar 2006 13:57 GMT
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
Jonathan West - 23 Mar 2006 14:03 GMT
> Hi,
>
[quoted text clipped - 3 lines]
> 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

Signature

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 

Klaus Linke - 25 Mar 2006 13:32 GMT
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

>> Hi,
>>
[quoted text clipped - 10 lines]
>    .Execute Replace:=wdReplaceAll
> End With
Jarryd - 27 Mar 2006 10:25 GMT
Hi Jonathan and Klaus.  All sorted now, thanks to you.

Jarryd
> If you want to avoid deleting longer strings containing a "7", use
> ".MatchWildcards = True" and ".Text = "<7[0-9]{4}>".
[quoted text clipped - 27 lines]
>>    .Execute Replace:=wdReplaceAll
>> End With
Greg - 23 Mar 2006 14:07 GMT
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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.