View Single Post
 
Old 03-12-2022, 09:59 PM
FabioDD FabioDD is offline Windows 10 Office 2021
Novice
 
Join Date: Mar 2022
Posts: 5
FabioDD is on a distinguished road
Default A macro to find interjections from a list and italicize them

Hello there! I'm trying to create a a macro that
- finds interjections from a list, like 'uh' or 'ah',
- with its punctuation, so it can be 'uh,' or 'uh!' or 'uh?',
- and then turns the interjection and its punctuation both italic.
BUT it cant italicize the 'ah' portion of a word like 'dahlia'
It has to work to both uppercase and lowercase and keep the case

I can do it with lots of find/replace recorded in a macro, but I would like a more elegant solution that I can add new interjections easily. I created a macro that almost worked, but it doesnt allow me to use < to mark the beginning of the word, so it always italicize 'ah' inside other words, like 'dahlia'
I'm not a programmer, so I rely on solutions I find in the web, but couldnt find one to do this to me...

Any suggestion?

My tries:
lots of this, one to each interjection - insane to keep

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
.Text = "<ah?"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

AND

this one, that replaces AH inside DAHLIA

Sub test()
Const strList As String = "ah^?|uh^?"
Dim vChar As Variant
Dim i As Long
Dim oRng As Range
vChar = Split(strList, "|")
For i = LBound(vChar) To UBound(vChar)
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(vChar(i))
oRng.Start = oRng.Start - 1
If oRng.Font.Italic = Not True Then
oRng.Font.Italic = True
End If
oRng.Collapse 0
Loop
End With
Next i
lbl_Exit:
Exit Sub
Reply With Quote