PDA

View Full Version : Need a macro for find and change bold



Rakesh
09-22-2010, 05:01 AM
Hi Guys,

I had a word document contains text with bold, italics and bold italics. I tried it to do manually by using find and replace. But I failed. I able to change only the formatting.

So I need a macro to insert tags before and after the bold, italic and bold italic text.


Example:
<bold>Bold Text</bold>
<ital>Italic Text</ital>
<bital>Bold Italic Text</bital>

Thanks,
Rakesh

fumei
09-22-2010, 11:12 AM
Stop trying to use Word as a HTML editor.

Plus: "I had a word document contains text with bold, italics and bold italics. I tried it to do manually by using find and replace. But I failed. I able to change only the formatting. "

I do not understand. Those ARE formatting. What do you mean by "only"?

I keep repeating myself. If you are using Styles (in this case character styles) this would be EASY. However, it sure seems you are not, so it becomes a little difficult. What have you tried so far?

here are some hints:

Searching for Bold would get both the Bold and the Bold Italic. So if you add the tags for your Bold search, you end up with:

<bold>Bold Text</bold>
<bold>Bold Italic Text</bold>

so you have to test for the italic IN your Bold search.

Can it be done? Yes. It is not all that hard. But, note that even with the correct search and adding of the tags, the tags themselves will likely take on the font attributes of the found text, like this:

<bold>Bold Text</bold>
<ital>Italic Text</ital>
<bital>Bold Italic Text<bital>

This can be easily fixed with:
Sub FixBracketsFonts()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.MatchWildcards = True
.ClearFormatting
.Text = "[<]*[>]"
Do While .Execute(Forward:=True) = True
r.Font.Bold = False
r.Font.Italic = False
Loop
End With
End Sub
This turns the above text into:

<bold>Bold Text</bold>
<ital>Italic Text</ital>
<bital>Bold Italic Text<bital>

Click "Add Tags" on the top toolbar of the demo.

Rakesh
05-15-2011, 12:30 AM
Hi Fumei,

Can I have the coding to insert the tags?


Example:
<bold>Bold Text</bold>
<ital>Italic Text</ital>
<bital>Bold Italic Text</bital>

Regards,
Rakesh