Replace text strings with different format with VBA

J

John Svendsen

Hi All,

I've put together VBA code that will find and replace text string pairs from
a file (see code below). However, all find and replace text strings end up
with the same format as the first character of the find string.
Is there a way I can introduce different formatting in this (e.g., find
string "aaa bbb ccc" [where bbb is bold/underscore/etc] and replace with
string "111 222 333 444" [where 333 is bold/underscore/etc]) in this macro?
Or, doe anyone have a better idea?

Thank you very much for your attention and time.

Rgds, JS

Sub po2en()
Dim sFirst, sLast As String
Open "c:\po2en.txt" For Input As #1
Do While Not EOF(1)
Input #1, sFirst, sLast
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = sFirst
.Replacement.Text = sLast
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop
Close #1
End Sub
 
T

Tony Jollans

In a word: no. How would you know whether to bold 222 or 333 in your
example?

You could do it in code if both the find and replace strings had the same
number of words and bolding was only applied to whole words - or if you had
some other clearly defined rules but there are many possible combinations.
 
J

John Svendsen

Hi Tony, thanks for replying.

I was afraid I'd hear some bad news...

However, is there a way to insert control chars word's find&repleace would
understand? Similar toHTML or some other way?

Again thanks for your answer :)

Rgds, JS


Tony Jollans said:
In a word: no. How would you know whether to bold 222 or 333 in your
example?

You could do it in code if both the find and replace strings had the same
number of words and bolding was only applied to whole words - or if you had
some other clearly defined rules but there are many possible combinations.

--
Enjoy,
Tony


John Svendsen said:
Hi All,

I've put together VBA code that will find and replace text string pairs from
a file (see code below). However, all find and replace text strings end up
with the same format as the first character of the find string.
Is there a way I can introduce different formatting in this (e.g., find
string "aaa bbb ccc" [where bbb is bold/underscore/etc] and replace with
string "111 222 333 444" [where 333 is bold/underscore/etc]) in this macro?
Or, doe anyone have a better idea?

Thank you very much for your attention and time.

Rgds, JS

Sub po2en()
Dim sFirst, sLast As String
Open "c:\po2en.txt" For Input As #1
Do While Not EOF(1)
Input #1, sFirst, sLast
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = sFirst
.Replacement.Text = sLast
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop
Close #1
End Sub
 
H

Helmut Weber

Hi John,

in theory you can search for anything
clearly defined and and replace it with anything,
clearly defined.
But not in one step.
However, is there a way to insert control chars
word's find&repleace would understand?
Similar toHTML or some other way?

No, not in the way you would like.

But you could search for:
3 letters and a space
followed by
3 letters and a space
followed by
3 letters.
If it that is what you want! Take care!

Start a search in the document's range.
Lets call it range(1).
If found, remember the range of the found string,
lets call it range(2).
Start a search in range(2).
If found, remember the range of the found string.
Lets call it range(3).
And so on at nauseam, in principle.

If something is clearly defined, you can find it,
but it would be a recursive process.
Google for: "Hamiltonian Path Problem".

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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