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

Tip: Looking for answers? Try searching our database.

Find and replace within a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jay - 13 Oct 2005 11:10 GMT
Hi,

I am extracting individual paragraphs from a source document. I want
to copy these paragraphs to another document, after performing a
find/replace on them. I am working on unformatted text. Is there a way
to do a find and replace on a string? The Word Help seems to imply
that this only works for ranges or selections. If I make a
find/replace on a range, then presumably this will affect my source
document, but I don't want this to happen. Also, according to the Help
(if I understand it correctly) the Find changes the range to the word
that is found, which is not what I want - I want the range to remain
as the entire paragraph. Here's some "pseudo-code" of what I wish to
do...

Loop paragraphs
 Extract text-only from paragraph from source document
 Replace various words with other words in the extracted paragraph
(but do not alter the source document)
 Put modified paragraph into destination document
End Loop

Thanks,

Jay
Greg - 13 Oct 2005 12:31 GMT
Jay,

Yes you can replace in a string:

Sub Test()
Dim oStr As String
oStr = "Now is the time for all good men" _
      & " to come to the aid of their country."
oStr = Replace(oStr, "men", "women")
MsgBox oStr
End Sub

Seems to me it would be easier to go ahead and extract form the source,
relace in the destination and then do the find and replace.
Jay - 13 Oct 2005 13:59 GMT
Thanks Greg. I tried that with Word 97, but "Replace" gave me a "Sub
or Function not Defined" compile error. Is it only supported in later
versions of Word (if so, I can use W2000 instead)? Also, is wildcard
search/replace possible with Replace()?

"Seems to me it would be easier to go ahead and extract form the
source, relace in the destination and then do the find and replace." I
don't copy into the destination document before doing the find/replace
because if after doing the find/replace, the paragraph is empty, I
will not put the empty paragraph into the destination, and I won't not
increment a VBA count variables. OK, I could still do this if I copied
into the destination then did the find and replace, but I guess it
would be a bit messy. However, if I have to do this, then I will do
it.

Example of three paragraphs that need to be copied with find/replace
(please ignore the line breaks that my emailer will probably
introduce!)

1) This line get copied in full from source to destination

2) This line get partly copied across <this bit within the tag
doesn't> to the destination

<3) Because this line is completely within the tags, it does not get
copied across, not even a paragraph mark, and the count variable
within VBA is not incremented>

The wildcard replace would be:
Text: "\<*\>"
Replacewith: ""

Then I would do further find/replace to clean up of repeated spaces,
leading space, trailing space, etc

Thanks,

Jay

> Jay,
>
[quoted text clipped - 10 lines]
> Seems to me it would be easier to go ahead and extract form the source,
> relace in the destination and then do the find and replace.
Tony Jollans - 13 Oct 2005 14:19 GMT
Yes, Replace was introduced in VBA 6 with Word 2000. If you have Word 2000
then fine, if not I can post a routine which does the same in 97.

As for what you're doing - will you actually have tags as in your example or
is it really more complicated?

--
Enjoy,
Tony

> Thanks Greg. I tried that with Word 97, but "Replace" gave me a "Sub
> or Function not Defined" compile error. Is it only supported in later
[quoted text clipped - 50 lines]
> source,
> > relace in the destination and then do the find and replace.
Jay - 13 Oct 2005 14:56 GMT
Hi Tony,

Although I do have Word 2000, it would be nice to make it compatible
with 97, so I'd be grateful if you'd post a routine - thanks.

The tags are as shown in my example (although maybe I'll use different
characters instead of < and >). It's possible that in the future I'll
make it more complicated, but for now, it is as shown.

Best wishes,

Jay

> Yes, Replace was introduced in VBA 6 with Word 2000. If you have Word 2000
> then fine, if not I can post a routine which does the same in 97.
[quoted text clipped - 60 lines]
> > source,
> > > relace in the destination and then do the find and replace.
Tony Jollans - 13 Oct 2005 15:16 GMT
Here you go ....

Function Replace(ByVal strOriginal As String, _
                ByRef strReplaceText As String, _
                ByRef strReplaceWith As String) _
            As String

   Dim Pos As Integer

   If Len(strReplaceText) = 0 Then

       Replace = strOriginal

   Else

       Replace = ""

       Do While strOriginal <> ""

           Pos = InStr(strOriginal, strReplaceText)

           If Pos = 0 Then ' not found
               Replace = Replace & strOriginal
               strOriginal = ""
           Else
               Replace = Replace & Left$(strOriginal, Pos - 1) &
strReplaceWith
               strOriginal = Mid$(strOriginal, Pos + Len(strReplaceText))
           End If

       Loop

   End If

End Function

--
Enjoy,
Tony

> Hi Tony,
>
[quoted text clipped - 86 lines]
> > > source,
> > > > relace in the destination and then do the find and replace.
Tony Jollans - 13 Oct 2005 15:54 GMT
... but a VBA Replace isn't really what you want for this as it has no
wildcard facility.

Here is an amended version of the macro which strips out text as marked in
your example

Function RemoveText(ByVal strOriginal As String) As String

   Dim Pos As Integer

   RemoveText = ""

   Do While strOriginal <> ""

       Pos = InStr(strOriginal, "<")

       If Pos = 0 Then ' not found
           RemoveText = RemoveText & strOriginal
           strOriginal = ""
       Else
           RemoveText = RemoveText & Left$(strOriginal, Pos - 1)

           Pos = InStr(Pos, strOriginal, ">")
           If Pos = 0 Then ' not found - assume at end
               strOriginal = ""
           Else
               strOriginal = Mid$(strOriginal, Pos + 1)
           End If

       End If

   Loop

End Function

--
Enjoy,
Tony

> Here you go ....
>
[quoted text clipped - 126 lines]
> > > > source,
> > > > > relace in the destination and then do the find and replace.
Jay - 14 Oct 2005 10:19 GMT
Thanks for the routines Tony.

After removing the tags, I also need to do some cleaning up (eg Find:
" {2,}", Replace: " " to remove repeated spaces). I can write a
routine to do this so that wildcard find/replace is not needed, since
it is not supported by the VBA Replace().

Since I've run into this problem before and probably will in the
future, I'd like to find out more about Find/Replace using ranges. Is
it possible to generate a range such that altering it does not alter
the source document? In other words, can I effectively copy a range
into a variable and manipulate it without affecting the document? I
get the feeling that I cannot - that the range is acting like a
pointer (to give a C analogy) rather than a complete independent copy
of the contents of the range. If this is true, then I guess that I
have to copy the range into another document, or onto the end of a
document, manipulate this, then clean up afterwards.

Thanks Tony, and everyone else, for your help,

Jay

> ... but a VBA Replace isn't really what you want for this as it has no
> wildcard facility.
[quoted text clipped - 34 lines]
> Enjoy,
> Tony
Tony Jollans - 14 Oct 2005 19:04 GMT
Hi Jay,

Yes, the Range object is a (dynamic) part of a Document and, AFAIK, cannot
exist independently. The Range variable *is* a pointer to the Range object.

If you want to work with the textual contents of a range you can copy that
to a (string) variable.

Recent versions of some objects (e.g. Font) have a Duplicate property which
allows limited manipulation without affecting the original. The Range object
actually has a Duplicate property but it returns a pointer to the original
Range still in the document so isn't really any help to you.

--
Enjoy,
Tony

> Thanks for the routines Tony.
>
[quoted text clipped - 58 lines]
> > Enjoy,
> > Tony
Jay - 17 Oct 2005 09:10 GMT
Thanks Tony,

"If you want to work with the textual contents of a range you can copy
that to a (string) variable"

That's what I've done for this problem, and it's working fine. But if
I'd wanted to use wildcard find/replace, then this wouldn't have been
an option, and I guess I would have had to copy the text to somewhere
else in the document or to another document, and use find/replace on a
range.

Thanks again, my knowledge of Word VBA is improving all the time.

Best wishes,

Jay

> Hi Jay,
>
[quoted text clipped - 34 lines]
> >
> > Jay

Rate this thread:






 
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.