Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-01-2021, 01:30 AM
tanko tanko is offline Macro Help - Wrap a term with characters and formatting Windows 10 Macro Help - Wrap a term with characters and formatting Office 2016
Novice
Macro Help - Wrap a term with characters and formatting
 
Join Date: Jan 2021
Posts: 2
tanko is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 01-01-2021, 03:54 AM
gmayor's Avatar
gmayor gmayor is offline Macro Help - Wrap a term with characters and formatting Windows 10 Macro Help - Wrap a term with characters and formatting Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,344
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

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
Reply With Quote
  #3  
Old 01-02-2021, 02:26 PM
tanko tanko is offline Macro Help - Wrap a term with characters and formatting Windows 10 Macro Help - Wrap a term with characters and formatting Office 2016
Novice
Macro Help - Wrap a term with characters and formatting
 
Join Date: Jan 2021
Posts: 2
tanko is on a distinguished road
Default

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!
Reply With Quote
Reply

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
Macro Help - Wrap a term with characters and formatting Subtract value if cell contains X term Jue92 Excel 4 11-01-2016 01:45 AM
Macro Help - Wrap a term with characters and formatting Macro to insert multiple pictures to word to a specific size and text wrap mescaL Word VBA 3 11-03-2014 10:51 PM
Macro Help - Wrap a term with characters and formatting Insert input box into macro to allow user to define search term Hoxton118 Word VBA 3 05-19-2014 02:03 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 07:43 AM.


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