Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-25-2019, 02:41 PM
WVA WVA is offline Macro to apply a style between quotations Windows 10 Macro to apply a style between quotations Office 2016
Novice
Macro to apply a style between quotations
 
Join Date: Jul 2019
Posts: 4
WVA is on a distinguished road
Default Macro to apply a style between quotations

I am trying to create a macro that applies a style to words between quotation marks. The macro should only work for words that are capitalized as well. For example "Definition" would get the style applied to it, but "definition" wouldn't. I've gotten a find and replace to work but only for the first example in a document. I'd like the macro to loop through the entire document. Any help would be greatly appreciated.

Thanks,

Sub Demo()

Selection.HomeKey Unit:=wdStory

With Selection.Find
.ClearFormatting


.Text = "[^0147][A-Z]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With

If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
' switch on selection extend mode
Selection.Extend

With Selection.Find
.ClearFormatting
.Text = "[^0148]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute

If Selection.Find.Found Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Style = ActiveDocument.Styles("Defined Terms Char")
End If
End With
End If
End Sub
Reply With Quote
  #2  
Old 07-25-2019, 02:53 PM
gmaxey gmaxey is offline Macro to apply a style between quotations Windows 10 Macro to apply a style between quotations Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub Demo()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Text = "(^0147)([A-Z]*)([^0148])"
    .MatchWildcards = True
    While .Execute
      oRng.Start = oRng.Start + 1
      oRng.End = oRng.End - 1
      oRng.Style = ActiveDocument.Styles("Strong") 'replace with your style
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
Reply With Quote
  #3  
Old 07-29-2019, 06:29 AM
WVA WVA is offline Macro to apply a style between quotations Windows 10 Macro to apply a style between quotations Office 2016
Novice
Macro to apply a style between quotations
 
Join Date: Jul 2019
Posts: 4
WVA is on a distinguished road
Default

Thanks. It works with the strong style but when I add my style into the macro it applies to the entire paragraph. Could there be some setting in my Style causing it to do that?

EDIT: Nevermind! I figured out the issue with my Style. Thanks again

Last edited by WVA; 07-29-2019 at 06:35 AM. Reason: Fixed
Reply With Quote
  #4  
Old 07-29-2019, 06:42 AM
gmaxey gmaxey is offline Macro to apply a style between quotations Windows 10 Macro to apply a style between quotations Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Yes. Your style needs to be a character style (not paragraph).
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 07-30-2019, 09:50 AM
WVA WVA is offline Macro to apply a style between quotations Windows 10 Macro to apply a style between quotations Office 2016
Novice
Macro to apply a style between quotations
 
Join Date: Jul 2019
Posts: 4
WVA is on a distinguished road
Default

I've been using this same macro to try to change a style for the first sentence of each paragraph. However, because I have a ^13 wildcard in the find, the macro only works if the paragraph before is in the same style. I'd like for it to change the style for the first sentence of a paragraph but starting after the ^13.

Here is my macro so far:


Sub Demo()

Dim aRng As Range
Set aRng = ActiveDocument.Range

With aRng.Find
.ClearFormatting
.Text = "(^13)(*)(.)"
.MatchWildcards = True
.Style = ActiveDocument.Styles("Heading 2")
While .Execute
aRng.Start = aRng.Start + 1
aRng.End = aRng.End - 1
aRng.Style = ActiveDocument.Styles("Heading 2 Title Char")
aRng.Collapse wdCollapseEnd
Wend

End With

End Sub
Reply With Quote
  #6  
Old 07-30-2019, 10:32 AM
gmaxey gmaxey is offline Macro to apply a style between quotations Windows 10 Macro to apply a style between quotations Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You might try:

Code:
Sub Demo()
Dim oRng As Range
Dim oRngDup As Range
  Set oRng = ActiveDocument.Range
    With oRng.Find
      .ClearFormatting
      .Style = ActiveDocument.Styles("Heading 2")
      Do While .Execute
        Set oRngDup = oRng.Duplicate
        oRngDup.Collapse wdCollapseStart
        oRngDup.MoveEndUntil ".", wdForward
        oRngDup.End = oRngDup.End + 1
        oRngDup.Style = ActiveDocument.Styles("Strong")
        oRng.Collapse wdCollapseEnd
        If oRng.End + 1 = ActiveDocument.Range.End Then Exit Do
      Loop
   End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 07-30-2019, 11:13 AM
WVA WVA is offline Macro to apply a style between quotations Windows 10 Macro to apply a style between quotations Office 2016
Novice
Macro to apply a style between quotations
 
Join Date: Jul 2019
Posts: 4
WVA is on a distinguished road
Default

This worked well except I don't like it applying to the period at the end of the sentence. I added an extra line after oRngDup.End = oRngDup.End + 1 but switched the + to a -. Now it's perfect.

Thanks again for the help!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
My style keeps changing, can't get saved style to apply Meenie50 Word 7 07-20-2017 03:47 PM
Macro to apply a style between quotations Apply Heading Style When Combining Documents snot369 Word 1 01-13-2017 11:04 AM
Is it possible to have a style apply two formats based upon text? DMcCollum Word 3 05-02-2015 06:29 PM
Macro to apply a style between quotations Can I apply a style to an image? humbleosity Word 1 01-14-2014 09:38 AM
Macro to apply a style between quotations Macro to apply style to selected tables ubns Word 1 08-02-2012 04:09 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:38 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