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 / February 2005

Tip: Looking for answers? Try searching our database.

Replacing empty strings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Neil Fraser - 02 Feb 2005 11:26 GMT
Hello,

A scripted search and replace in a Word document works fine when
replacing text with other text.  But it doesn't do anything when
replacing text with an empty string.

 With WordDoc.Selection.Find
     .ClearFormatting
     .Replacement.Font.ColorIndex = wdAuto
     .Replacement.Font.Bold = False
     .Text = "test"
     .Replacement.Text = ""
     .Forward = True
     .Wrap = wdFindContinue
     .Format = True
     .MatchCase = False
     .MatchWholeWord = False
     .MatchByte = False
     .MatchWildcards = False
     .MatchSoundsLike = False
     .MatchAllWordForms = False
     .MatchFuzzy = False
     .Execute Replace:=wdReplaceAll
 End With

Change
 .Replacement.Text = ""
to
 .Replacement.Text = " "
and it will replace "test" with a space perfectly.  But there doesn't
seem to be a way to replace "test" with nothing.  Any suggestions on
how to delete text?
Dave Lett - 02 Feb 2005 12:18 GMT
Hi Neil,

Apparently, Word is getting hung on the fact that you want to format the ""
replacement. The following will remove "test":

 With WordDoc.Selection.Find
     .ClearFormatting
     .Replacement.ClearFormatting
     .Text = "Test"
     .Replacement.Text = ""
     .Forward = True
     .Wrap = wdFindContinue
     .Execute Replace:=wdReplaceAll
 End With

HTH,
Dave

> Hello,
>
[quoted text clipped - 28 lines]
> seem to be a way to replace "test" with nothing.  Any suggestions on
> how to delete text?
neil.fraser@gmail.com - 02 Feb 2005 12:46 GMT
> Apparently, Word is getting hung on the fact that you want to
> format the "" replacement. The following will remove "test":

Thanks!  That worked perfectly.  I'll selectively apply formatting only
if there is a non-null replacement string.
Wonder if this is another Word bug...
Klaus Linke - 04 Feb 2005 15:00 GMT
> > Apparently, Word is getting hung on the fact that you want to
> > format the "" replacement. The following will remove "test":

> Thanks!  That worked perfectly.  I'll selectively apply formatting only
> if there is a non-null replacement string.
> Wonder if this is another Word bug...

It works the same way in the user interface.

If you specify some formatting, you don't need to enter "^&" in "Replace with:".
That is kind of neat if you often apply formatting with "Find/Replace".

Since you can leave "Find what:" empty, too, to search for some specific
formatting, it seems like a sensible convention.
It can bite you though, if you aren't aware of it...

Regards,
Klaus
 
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.