#1
|
|||
|
|||
Macro Help - Wrap a term with characters and formatting
Hi, I am a noob and need help making a macro to do the following: I'd like to take a word or phrase and use a macro to wrap it with parenthesis and quotation marks, and make it bold. For example, the word Building in this sentence would become ("Building"). Or the phrase Effective Date becomes ("Effective Date"). It would have to work within an existing paragraph. I have zero VBA skills, and I tried to record a macro for this purpose, but wasn't able to make it work. Thank you! |
#2
|
||||
|
||||
It is fairly straightforward and works best with ranges which the recorder doesn't use:
Code:
Sub AddEmphasis() Dim oRng As Range Set oRng = Selection.Range If Len(oRng) = 0 Then MsgBox "Nothing selected", vbCritical GoTo lbl_Exit End If With oRng 'avoid inadvertently selected spaces at start and end of the selection .MoveEndWhile Chr(32), wdBackward .MoveStartWhile Chr(32) 'make selection bold .Font.Bold = True 'add parenthesis and quotation marks .InsertBefore Chr(40) & Chr(34) .InsertAfter Chr(34) & Chr(41) 'optional remove bold emphasis from parenthesis and quotation marks .Characters.First.Bold = False .Characters.First.Next.Bold = False .Characters.Last.Bold = False .Characters.Last.Previous.Bold = False End With lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Works perfectly, thank you so much! I really should learn VBA, but it's tough to fit it in.
FYI, I ended up replacing the quotation mark characters with smart quotes because legal forms typically use curly quotes. Seems to be working fine! |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Formatting ID numbers to insert dashes in between some characters | shaun0406 | Word VBA | 6 | 12-22-2020 09:36 AM |
Macro to wrap each section in a document with some text | mattbrad | Word VBA | 3 | 07-22-2019 03:13 AM |
Subtract value if cell contains X term | Jue92 | Excel | 4 | 11-01-2016 01:45 AM |
Macro to insert multiple pictures to word to a specific size and text wrap | mescaL | Word VBA | 3 | 11-03-2014 10:51 PM |
Insert input box into macro to allow user to define search term | Hoxton118 | Word VBA | 3 | 05-19-2014 02:03 AM |