Need an HTML replacement Macro

R

rwagner

I need to perform a find/replace in a Word doc so that text encased in
a specific html tag is replaced with a style mathing that tag. For
instance, this:

<em>some text here</em>
<em>different text in this one</em>

should be replaced with this:

some text here <--(image that this is bold/italics)
different text in this one <-- (same comment)

I'm having trouble figuring this one out. Thanks in advance for any
guidance.
 
F

Fumei2 via OfficeKB.com

Please clarify. Is this HTML source code, or text in a Word document? In
other words, is the TEXT <em> there, or not?

If I understand correctly, you have:

<em>some text here</em> as TEXT

and you want to remove the <em> and </em> - as TEXT - and format everything
between them as bold & italic.

Yes?

The reason I ask is: "that text encased in a specific html tag" is
contradictary.

If it is a text document, then the "<em>" and the "</em>" is plain text, and
the "some text here" is NOT encased at all.
 
P

Pesach Shelnitz

Hi,

If you opened the HTML file as text in Word and the <em> and </em> tags are
visible, the following macro will remove the tags and format the text between
them as bold.

Sub ReplaceTagByBold()
Dim MyRange As Range
Dim pos As Long

Set MyRange = ActiveDocument.Range
With MyRange.Find
Do While .Execute(findText:="\<em\>*\</em\>", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
MyRange.Font.Bold = True
pos = MyRange.start
MyRange.Collapse Direction:=wdCollapseEnd
MyRange.MoveStart wdCharacter, -5
MyRange.Delete
MyRange.start = pos
MyRange.End = pos + 4
MyRange.Delete
Loop
End With
Set MyRange = Nothing
End Sub
 
R

rwagner

Hi,

If you opened the HTML file as text in Word and the <em> and </em> tags are
visible, the followingmacrowill remove the tags and format the text between
them as bold.

Sub ReplaceTagByBold()
    Dim MyRange As Range
    Dim pos As Long

    Set MyRange = ActiveDocument.Range
    With MyRange.Find
        Do While .Execute(findText:="\<em\>*\</em\>", _
            MatchWildcards:=True, _
            Wrap:=wdFindStop, Forward:=True) = True
            MyRange.Font.Bold = True
            pos = MyRange.start
            MyRange.Collapse Direction:=wdCollapseEnd
            MyRange.MoveStart wdCharacter, -5
            MyRange.Delete
            MyRange.start = pos
            MyRange.End = pos + 4
            MyRange.Delete
        Loop
    End With
    Set MyRange = Nothing
End Sub

Fumei2, yes, your assumption is correct. "Encased" is just the word
that seemed to fit best. :)

Pesach, that is completely amazing! It totally works! I started by
trying to define a wildcard search but failed miserably - I kept only
getting the "tag" text. I tried the search terms in your macro in the
"find" dialog and (of course) it picks up the exactly the text it
should. And I see your approach here is just to delete the "tags"
from the ends rather than extract the text from between them like you
would in a regex.

I have to say, I'm fair at a handful of programming languages, but
Word Macros have got me pulling my hair out. Thanks for making this
one easy for me. I'll have no trouble adapting this for other tags.

-Rob
 

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