Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-12-2022, 09:59 PM
FabioDD FabioDD is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2021
Novice
A macro to find interjections from a list and italicize them
 
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
  #2  
Old 03-13-2022, 01:35 AM
gmayor's Avatar
gmayor gmayor is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Rather than re-invent the wheel, take a look at Document Batch Processes and its Find and Replace (with table defined formatting) option.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-13-2022, 06:22 AM
FabioDD FabioDD is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2021
Novice
A macro to find interjections from a list and italicize them
 
Join Date: Mar 2022
Posts: 5
FabioDD is on a distinguished road
Default

Hello there. Thanks for the tip, but as far as I could see, to replace all the interjections with all the pontuaction that may follow them, I have to add them multiple times, like ah! ah. ah... ah? ah, - not a single time for each, and one for each case AH! AH. AH... AH? AH, - and maybe one more if it has mixed case, like Ah! - am I right?

If I could use '<' in the second example, to mark the beginning of the word, it would work (I guess), but it doesnt accept the '<' wildcard.
Reply With Quote
  #4  
Old 03-13-2022, 09:55 PM
gmayor's Avatar
gmayor gmayor is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

That would be the principle. However you could use wildcards with minimal iterations in a macro using wildcards Replace using wildcards e.g.
Code:
Sub Macro1()
Const sFind As String = "<ah[\!\?\,.]{1,}|<AH[\!\?\,.]{1,}|<Ah[\!\?\,.]{1,}"
Const sRepl As String = "^&"
Dim oRng As Range
Dim vFind As Variant
Dim i As Integer
    vFind = Split(sFind, "|")
    For i = 0 To UBound(vFind)
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(findText:=vFind(i), _
                              MatchWildcards:=True, _
                              ReplaceWith:=sRepl)
                oRng.Font.Italic = True
                oRng.Collapse 0
            Loop
        End With
    Next i
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 03-14-2022, 05:41 AM
FabioDD FabioDD is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2021
Novice
A macro to find interjections from a list and italicize them
 
Join Date: Mar 2022
Posts: 5
FabioDD is on a distinguished road
Default

Thank you very much, it works for me if I take out the {1,} in Const sFind As String = "<ah[\!\?\,.]{1,}|<AH[\!\?\,.]{1,}|<Ah[\!\?\,.]{1,}"

It doesn't run if it has this bit, it gives me Error 5560. Without it, it works! Why? I have no clue.

I will try to work a bit more on it, thanks for the light.
Reply With Quote
  #6  
Old 03-14-2022, 07:04 AM
gmayor's Avatar
gmayor gmayor is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Try instead
Code:
Dim sFind As String
sFind = "<ah[\!\?\,.]{1,}|<AH[\!\?\,.]{1,}|<Ah[\!\?\,.]{1,}"
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 03-14-2022, 08:41 AM
FabioDD FabioDD is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2021
Novice
A macro to find interjections from a list and italicize them
 
Join Date: Mar 2022
Posts: 5
FabioDD is on a distinguished road
Default

Still gaves error 5560 ><

Looking around I found it may be because my system language is not set to English.
Reply With Quote
  #8  
Old 03-14-2022, 10:03 PM
gmayor's Avatar
gmayor gmayor is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

In that case change the commas in {1,} to your local list separator character - almost certainly a semi-colon thus {1;}
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 03-15-2022, 06:28 AM
FabioDD FabioDD is offline A macro to find interjections from a list and italicize them Windows 10 A macro to find interjections from a list and italicize them Office 2021
Novice
A macro to find interjections from a list and italicize them
 
Join Date: Mar 2022
Posts: 5
FabioDD is on a distinguished road
Default

Success! Thank you very much for your time ♥
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
A macro to find interjections from a list and italicize them Macro to Find & Highlight Words from List DRD992 Word VBA 15 06-17-2023 05:06 AM
A macro to find interjections from a list and italicize them Use VBA to find all cells with the same value and add to list trevorc Excel Programming 2 11-24-2021 06:51 PM
Find - Replace Macro using a table list mdw Word 0 08-01-2013 04:36 PM
A macro to find interjections from a list and italicize them Macro that can find phrase and then find another and copy jperez84 Word VBA 10 09-19-2012 04:48 PM
A macro to find interjections from a list and italicize them How to write a macro to find a specified name in a list of data? Jaffa Excel 1 10-23-2010 02:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:34 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft