Insert a string between two strings

J

John Smith

I would like to find two strings in a paragraph and insert
parentheses "()" between them if there isn't a "(" or ")" already
in between. These two strings are not words but partial words.

Examples:

string1= univ string2=cal

university california => university () california
universe (no) califlower => do nothing, already has (
university Washington => no match, do nothing

How can this be done?
 
W

Word Heretic

G'day John Smith <[email protected]>,

Any sequence of non-space characters followed by a standard
punctuation mark or space, is considered a Word object. Here is some
sample code to get you started

Public Sub BracketMagic()
Dim Para As Paragraph
Dim Inserter As Range

For Each Para In ActiveDocument.Paragraphs
If Para.Range.Words.Count = 3 Then '2+ 1 for end of para mark
Set Inserter = Para.Range.Words(2)
Inserter.Collapse wdCollapseStart
Inserter.InsertAfter "() "
End If
Next

If Not Inserter Is Nothing Then Set Inserter = Nothing
Set Para = Nothing
End Sub


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


John Smith reckoned:
 
C

Cindy M -WordMVP-

Hi John,

Start by recording the two searches using Edit/Find in a macro.
Take a close look at the Wildcard options (to pick up entire
words). you'll find some articles on find/replace, both in the UI
and with VBA, at word.mpvs.org

Once you've recorded the macros, you need to replace using
Selection with the Range object, and you need to run each Find
using two separate Range object variables. This way, after both
Find.Executes have run, you'll be able to locate both and see
what's in between. Assign this Range using Range.End+1 from the
one, and Range.Start-1 from the other to define it. If there's only
a single space between, then the parentheses plus a space can be
inserted, otherwise you continue in the loop until nothing more is
found.

See how far the website takes you, then come back with specific
questions/problems.
I would like to find two strings in a paragraph and insert
parentheses "()" between them if there isn't a "(" or ")" already
in between. These two strings are not words but partial words.

Examples:

string1= univ string2=cal

university california => university () california
universe (no) califlower => do nothing, already has (
university Washington => no match, do nothing

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8
2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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