Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Macro to automatically bold or italicize specific text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan Neely - 09 Oct 2007 19:11 GMT
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.
Helmut Weber - 09 Oct 2007 21:11 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Dan Neely - 09 Oct 2007 21:42 GMT
> 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.
Helmut Weber - 09 Oct 2007 21:53 GMT
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?

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Dan Neely - 09 Oct 2007 22:04 GMT
> 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.

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.
Helmut Weber - 09 Oct 2007 22:28 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Russ - 10 Oct 2007 08:08 GMT
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

> Hi Dan,
>
[quoted text clipped - 31 lines]
> With pointed brackets interspersed at random in
> your text, things will get very difficult to handle.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Dan Neely - 10 Oct 2007 13:16 GMT
> 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.
Russ - 10 Oct 2007 19:11 GMT
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?

>> Dan,
>> Is everything coming in pre-tagged?
[quoted text clipped - 4 lines]
> but it's a step forward from having to do the same to the output every
> time a new version is generated.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Dan Neely - 10 Oct 2007 20:57 GMT
> 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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.