Find and replace within a string

J

Jay

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
 
G

Greg

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.
 
J

Jay

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
 
T

Tony Jollans

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?
 
J

Jay

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
 
T

Tony Jollans

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
 
T

Tony Jollans

... 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
 
J

Jay

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
 
T

Tony Jollans

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


Jay said:
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
 
J

Jay

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
 

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