how to italicize text between characters

K

Keith G Hicks

Word vba 2003.

Okay, this has to be easy but after an hour I'm not getting this to work. I
need to italicize text that's between ^ markers.

For example:

Now ^is^ the time for all ^good^ men to come to the aid of ^their^
country....

The ^ are ALWAYS in pairs. I need all the text that's between ^ to be in
italics. I'm seearching for the first instance of the ^ and I can find it
but then I need to set the range to be the text between (it can include the
^ too, that's okay) and then italicize it. Then I need to remove all the ^
in the entire doc.

Like I said, just not figuring out how to mark the end as the 2nd ^ in my
loop. Here's the mess I'm workign with:

With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
While .Execute(findtext:="^", MatchWildcards:=False)
Set oRng = Selection.Range
oRng.End = .Execute(findtext:="^", MatchWildcards:=False)
'oRng.End = oRng.Next(wdCharacter, 1).End
oRng.Font.Italic = True
If .Execute(findtext:="^", MatchWildcards:=False) Then
oRng.End = oRng.GoTo.Find
End If

'sNewTextFound = oRng
'If sNewTextFound <> sPrevTextFound Then
' oRng.InsertBefore vbCrLf
' sPrevTextFound = sNewTextFound
'End If
Wend
End With
End With

Keith
 
M

macropod

Hi Keith

Try using a wildcard Find/Replace with:
Find = '(^94[!^94]{1,}^94)'
Replace = '^&'
(making the replacement font bold and omitting the tick marks in both cases)

No macro required (though you could use one, somply recording the above).
 
G

Greg Maxey

Sub FindAndItalicBoundText()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "^^<*>^^"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.Characters.First.Delete
.Characters.Last.Delete
.Font.Italic = True
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub
 
M

macropod

To delete the '^' characters at the same time, try using a wildcard Find/Replace with:
Find = '^94([!^94]{1,})^94'
Replace = '\1'
(making the replacement font bold and omitting the tick marks in both cases)


--
Cheers
macropod
[Microsoft MVP - Word]


macropod said:
Hi Keith

Try using a wildcard Find/Replace with:
Find = '(^94[!^94]{1,}^94)'
Replace = '^&'
(making the replacement font bold and omitting the tick marks in both cases)

No macro required (though you could use one, somply recording the above).

--
Cheers
macropod
[Microsoft MVP - Word]


Keith G Hicks said:
Word vba 2003.

Okay, this has to be easy but after an hour I'm not getting this to work. I
need to italicize text that's between ^ markers.

For example:

Now ^is^ the time for all ^good^ men to come to the aid of ^their^
country....

The ^ are ALWAYS in pairs. I need all the text that's between ^ to be in
italics. I'm seearching for the first instance of the ^ and I can find it
but then I need to set the range to be the text between (it can include the
^ too, that's okay) and then italicize it. Then I need to remove all the ^
in the entire doc.

Like I said, just not figuring out how to mark the end as the 2nd ^ in my
loop. Here's the mess I'm workign with:

With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
While .Execute(findtext:="^", MatchWildcards:=False)
Set oRng = Selection.Range
oRng.End = .Execute(findtext:="^", MatchWildcards:=False)
'oRng.End = oRng.Next(wdCharacter, 1).End
oRng.Font.Italic = True
If .Execute(findtext:="^", MatchWildcards:=False) Then
oRng.End = oRng.GoTo.Find
End If

'sNewTextFound = oRng
'If sNewTextFound <> sPrevTextFound Then
' oRng.InsertBefore vbCrLf
' sPrevTextFound = sNewTextFound
'End If
Wend
End With
End With

Keith
 

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