Macro to automatically bold or italicize specific text

D

Dan Neely

I'm working with a 3rd party document generation system that only
stores plain text. Rather than having to manually go through the
document and bold/italicize certain phrases every time I'd like to
automate the process. How would I write a macro to do it? I'd like
to use HTML style tags to indicate the formatting but am flexible in
that regard.

before the macro is run:
This sentence contains <b>bold</b> and <i>italic</i> text.

After the macro was run the text would look like this but with the
words bold and italic bolded and italicized respectively:
This sentence contains bold and italic text.
 
H

Helmut Weber

Hi Dan,

I got a feeling, there must be an easier way. ;-)

Sub TEst444()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\<b\>*\</b\>"
.MatchWildcards = True
While .Execute
rDcm.start = rDcm.start + 3
rDcm.End = rDcm.End - 4
rDcm.Font.Bold = True
rDcm.Collapse direction:=wdCollapseEnd
Wend
End With
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\<i\>*\</i\>"
.MatchWildcards = True
While .Execute
rDcm.start = rDcm.start + 3
rDcm.End = rDcm.End - 4
rDcm.Font.Italic = True
rDcm.Collapse direction:=wdCollapseEnd
Wend
End With
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\<*\>"
.MatchWildcards = True
While .Execute
' rDcm.Select
rDcm.Font.Reset
rDcm.Collapse direction:=wdCollapseEnd
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

Dan Neely

Hi Dan,

I got a feeling, there must be an easier way. ;-)

It's not the prettiest code I've ever seen, but it looks like
something I should be able to hack into doing what I want. The change
I want to make would be to move the tag delete up to happen at the
same time as the reformat. Both to guard against bad input in the
form of broken tags, and Clever Users(tm) trying to put other HTML
tags into the source data on the assumption that they'll work as well.
 
H

Helmut Weber

Hi Dan,
The change I want to make would be
to move the tag delete up to happen at the same time as the reformat.

sorry, that is over my head as regards to my english.

You want to delete the tags?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Dan Neely

Hi Dan,


sorry, that is over my head as regards to my english.

Sorry, I'll try to be clearer.

You want to delete the tags?

Yes, I want to delete each pair of tags at the same time that I bold
or italicize the text between them. Anything that looks like a tag
but is not used in a formatting change should be left in place.
 
H

Helmut Weber

Hi Dan,

Sub Test444a()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "(\<b\>)(*)(\</b\>)"
.MatchWildcards = True
.Replacement.Text = "\2"
.Replacement.Font.Bold = True
.Execute Replace:=wdReplaceAll
End With
End Sub

Which will work for a situation,
where tags were applied systematically, like
<u><i><b>bold</b></i></u>
for underline, italic, bold.
Not at random.

You would have to process bold, italic, underline in that order.

Yes, I want to delete each pair of tags at the same time that I bold
or italicize the text between them.
See Above.
Anything that looks like a tag
but is not used in a formatting change should be left in place.

No way, almost.
Computers don't like fuzzyness like "like".

You could set up a list of allowed tags.
With pointed brackets interspersed at random in
your text, things will get very difficult to handle.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Russ

Dan,
Is everything coming in pre-tagged?

Or you manually applying tags first?
(I hope not, since you want to delete the tags, then tagging seems like an
unnecessary extra step.)

One useful tool in Word is the format painter icon (usually next to the cut,
copy, and paste icons).

---------------
From Word Help:
Copy all formatting from one object to another
1. Click the object that contains the formatting you want to copy.
2. Click Format Painter , and then click the object you want to copy the
formatting to.
Tip You can copy all formatting of an object to several objects by
double-clicking Format Painter, and then selecting several objects in
succession. Click Format Painter again when you're done.
See also
Copy only the look and style of text
---------------

And if you'd rather use the keyboard:

Copy formatting from text.
CTRL+SHIFT+C

Apply copied formatting to text.
CTRL+SHIFT+V
 
D

Dan Neely

Dan,
Is everything coming in pre-tagged?

Yes. I'm using a 3rd party document generation tool. While it
outputs to word format the backend only stores plain text in the
database. I'll have to manually tag the source data the first time,
but it's a step forward from having to do the same to the output every
time a new version is generated.
 
R

Russ

Dan,
Have you played around with saving a normally formatted Word .doc file as
..html or .xml from Word, storing that markup version in the database, and
then opening the file from the database into to Word again to see what
formatting sticks?
 
D

Dan Neely

Dan,
Have you played around with saving a normally formatted Word .doc file as
.html or .xml from Word, storing that markup version in the database, and
then opening the file from the database into to Word again to see what
formatting sticks?

It's a 3rd party (Rational) system and the frontend only takes text
(unicode?) input. The app doing the pull is from the same source, and
I don't have access to the DB directly. Even if I could muddle there,
writing a new entry app is out of the question, as is letting the
average user directly edit the table. Typing a few HTMLesque tags is
within the expected level of skill.
 

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