can a macro 1) find italics 2) insert code before & after text?

S

smith02

I'm trying to set-up a macro that will search a document for text in italics
and then insert a code (<I>) before and after the italized text. Is this
possible?
 
H

Helmut Weber

Hi Dave,

certainly there are several ways,
and there is nothing wrong with your solution.

But being involved with tagging for more than a decade,
I bet, after searching for italics,
we might soon read a question for tagging
bold, italic, underlined, subscript characters.

Therefore, I'd only remove the formating
which was in question.

Sub test09947()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Font.Italic = True
While .Execute
rTmp.Font.Italic = False
rTmp.InsertBefore "<i>"
rTmp.InsertAfter "</i>"
rTmp.Start = rTmp.End
rTmp.End = ActiveDocument.Range.End
Wend
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Dave Lett

Hi Helmut,
I'll defer, but I'm curious: why remove the formatting? I was thinking that
after tagging the text the user would save this Word file as a .txt file and
that would remove any formatting.
Dave
 
H

Helmut Weber

Hi Dave,
I'm curious: why remove the formatting? I was thinking that
after tagging the text the user would save this Word file
as a .txt file and that would remove any formatting.

It's only, if a user would not save the file as .txt
immediately after tagging, for whatever reason,
the tag at the end of a range would be tagged again.
</i></i></i>...
As tagging at the start of a range does not
take over the formatting of the range.
But tagging at the end of a range does.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Dave Lett

Yep, yep, yep. Thank you for clarifying.

Helmut Weber said:
Hi Dave,


It's only, if a user would not save the file as .txt
immediately after tagging, for whatever reason,
the tag at the end of a range would be tagged again.
</i></i></i>...
As tagging at the start of a range does not
take over the formatting of the range.
But tagging at the end of a range does.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

smith02

Dear Dave and Helmut: Thank you for your help. Both of your postings were
very helpful!
 

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