Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-23-2023, 05:32 AM
RRB's Avatar
RRB RRB is offline Need to automatically apply style within parentheses. Windows 11 Need to automatically apply style within parentheses. Office 2021
Susan Flamingo
Need to automatically apply style within parentheses.
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 261
RRB is on a distinguished road
Default 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)
Reply With Quote
  #2  
Old 03-23-2023, 07:00 AM
Charles Kenyon Charles Kenyon is offline Need to automatically apply style within parentheses. Windows 11 Need to automatically apply style within parentheses. Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,124
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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.
Reply With Quote
  #3  
Old 03-23-2023, 07:07 AM
RRB's Avatar
RRB RRB is offline Need to automatically apply style within parentheses. Windows 11 Need to automatically apply style within parentheses. Office 2021
Susan Flamingo
Need to automatically apply style within parentheses.
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 261
RRB is on a distinguished road
Default 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
Reply With Quote
  #4  
Old 03-25-2023, 01:56 PM
Guessed's Avatar
Guessed Guessed is offline Need to automatically apply style within parentheses. Windows 10 Need to automatically apply style within parentheses. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Note that I've chosen to limit the distance backwards that the macro searches for the starting bracket to 100 characters. You can fiddle this number if you want to search more or less characters.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 03-26-2023, 11:49 PM
RRB's Avatar
RRB RRB is offline Need to automatically apply style within parentheses. Windows 11 Need to automatically apply style within parentheses. Office 2021
Susan Flamingo
Need to automatically apply style within parentheses.
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 261
RRB is on a distinguished road
Default Thank You

Works great. Thank you
Reply With Quote
  #6  
Old 03-27-2023, 12:06 AM
RRB's Avatar
RRB RRB is offline Need to automatically apply style within parentheses. Windows 11 Need to automatically apply style within parentheses. Office 2021
Susan Flamingo
Need to automatically apply style within parentheses.
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 261
RRB is on a distinguished road
Default 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
Reply With Quote
  #7  
Old 03-27-2023, 08:34 PM
Guessed's Avatar
Guessed Guessed is offline Need to automatically apply style within parentheses. Windows 10 Need to automatically apply style within parentheses. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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

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
Need to automatically apply style within parentheses. Can I apply a style to an image? humbleosity Word 1 01-14-2014 09:38 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:32 AM.


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