#1
|
||||
|
||||
Need to automatically apply style within parentheses.
Hi friends! It has been so many years since I last programmed with VBA I'm going to take the audacious move to ask if somebody can help me write the following macro: I have text within parentheses and when I type the close parentheses "(" together with the alt key I want Word to select all the text back to the opening parentheses ")" and format it with style called "styMyStyle". And then the cursor moves back to where it originally was, after the closing parentheses. Thanks in advance very much! Susan (mods: I posted this in the VBA section but have not received any reply so I have moved it here. I don't know how to delete the post there. Plz let it stay here. Thank You) |
#2
|
|||
|
|||
Hi Susan,
Your definition of opening and closing parentheses is opposite mine. For me, the opening one would be: "(" and closing: ")" If you are doing this as you type, do you really need a macro? Type your parentheses, and select the text. Apply your style. You can do that with a keyboard shortcut attached to the style. Assigning Keyboard Shortcuts in Microsoft Word 2007-2021 (365) If you need a macro, do you want the style applied to the parentheses as well as the text? Last edited by Charles Kenyon; 03-23-2023 at 11:43 AM. |
#3
|
||||
|
||||
Macro help
Well, you are as usual, right.
It's just called laziness and wanting things to be automated. And yes I want to include the parentheses in the style formatting. Any and all help is highly appreciated. Susan |
#4
|
||||
|
||||
This macro works for me. You can assign a keyboard shortcut to run it by setting that shortcut as Alt+)
Code:
Sub CloseBracketsAndFormat() Dim aRng As Range Selection.TypeText ")" Set aRng = Selection.Range aRng.MoveStartUntil "(", -100 If Len(aRng.Text) > 2 Then aRng.Style = "styMyStyl" End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
||||
|
||||
Thank You
Works great. Thank you
|
#6
|
||||
|
||||
One last question
I noticed that it formats the text back up to the ")" but not the ")" itself. How do I include the open par in the selection for formatting?
Thankx Susan |
#7
|
||||
|
||||
You can move the start or finish of the range before applying the style eg
Code:
aRng.Start = aRng.Start - 1 aRng.End = aRng.End + 1
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Apply table style | shahid.majeed | Word Tables | 0 | 10-02-2019 01:25 AM |
Macro to apply a style between quotations | WVA | Word VBA | 6 | 07-30-2019 11:13 AM |
My style keeps changing, can't get saved style to apply | Meenie50 | Word | 7 | 07-20-2017 03:47 PM |
Automatically applying character style while using fast style feature | Didier Super | Word | 1 | 04-10-2017 05:10 AM |
Can I apply a style to an image? | humbleosity | Word | 1 | 01-14-2014 09:38 AM |