to replace Bold/Underlined text with itself and added something

A

alfonso gonzales

Hello,

I need to replace all instances of text formatted as Bold (and also
Underlined) with their text (as String)
but stripped of the formatting and nested into appropriate HTML markers for
Bold/Underlined text (Dim Txt as String [...] Txt = "<B>" & Txt & "</B>").

I would appreciate some help (or hints) regarding that task.

Alvaro E. Gonzales
 
D

Doug Robbins

Hi Alvaro,

Use:

Dim foundtext As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Font.Bold = True
.Font.Underline = True
.Format = True
Do While .Execute(FindText:="", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
Set foundtext = Selection.Range
foundtext.Font.Bold = False
foundtext.Underline = False
foundtext.InsertBefore "<B>"
foundtext.InsertAfter "</B>"
Loop
End With
Selection.HomeKey wdStory


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
H

Helmut Weber

Hi Alfonso,
like this:
Sub Test321()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
Resetsearch
With rDcm.find
.Text = ""
.Font.Bold = True
.Font.Underline = True
.MatchWildcards = True
.Replacement.Text = "<b><u>&</u></b>"
.Replacement.Font.Bold = False
.Replacement.Font.Underline = False
.Execute Replace:=wdReplaceAll
End With
Resetsearch
End Sub
'---
Sub Resetsearch()
With Selection.find
.Parent.Collapse
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
 
A

Alvaro Gonzales

Hi Helmut
I have checked your code, and see one problem as regards my purpose (sorry
if I stated it not clearly enough).
The snag is that your code replaces the original text with the markers
"<b><u>&</u></b>", while what I need is the original text, while stripped of
the bold or underline formating, nested in these tags, not removed entirely.
Greetings
Alvaro E. Gonzales
 
H

Helmut Weber

Hi Alfonso
it is
.Replacement.Text = "<b><u>^&</u></b>" '!
not
.Replacement.Text = "<b><u>&</u></b>"
Somehow I managed to loose the "^".
And if there are e.g. bold underlined paragraph marks,
it might get pretty complicated.
 
A

alfonso gonzales

Hi Helmut,
I am really greatful for this hint, as now I see the code working
flawlessly.
What's more, I approached each kind of formatting separately, as you
suggested. It is more clear that way. And easier to manipulate.
Greetings
A.E. Gonzales
 

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