Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to change font 2

Status
Not open for further replies.

CCHall

IS-IT--Management
May 3, 2005
46
US
Does anyone know if it would be possible to write a macro to go through a Word document, find everything that is enclosed in underscores (_), delete the underscores and make the enclosed text italics?

For example:

I am _going_ to get some lunch.

would become

I am going to get some lunch.

Any help greatly appreciated. This is a very long document that would take ages to edit manually. Thanks!
 
Yes it is possible.
Code:
Sub ReplaceAndFormat()
Selection.HomeKey Unit:=wdStory
With Selection.Find
    .ClearFormatting
    .MatchWildcards = True
        Do While (.Execute(Findtext:="[_]*[_]", Forward:=True) = True) = True
        With Selection
            .Text = Left(Right(Selection.Text, Len(Selection.Text) - 1), _
                Len(Selection.Text) - 2)
            .Font.Italic = True
            .Collapse Direction:=wdCollapseEnd
        End With
    Loop
End With
End Sub

Gerry
 

If they are words (as Word understands a word to be), AutoFormat will do this. It will change your example but will not, I think, change "g_oin_g" to "going"

Make sure "*Bold* and _Italic_ with real formatting" is checked under Tools > AutoCorrect Options > AutoFormat tab, then select Format > AutoFormat from the Menu.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Thanks so much, I really appreciate the assistance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top