Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2018, 03:32 PM
jeffreybrown jeffreybrown is offline Select pagagraphs between two words and run macro Windows Vista Select pagagraphs between two words and run macro Office 2007
Expert
Select pagagraphs between two words and run macro
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default Select pagagraphs between two words and run macro

This macro is working fine to select all of the paragraphs between two specific words, but it's also selecting the end of the first word thereby changing the style on the first word. I would only like the paragraphs in-between the two words selected.



Is there an adjustment that can be made with this macro?

Code:
Sub RevisedFindIt()
    Dim rng1 As Range
    Dim rng2 As Range
    Dim strTheText As String
    Set rng1 = ActiveDocument.Range
    If rng1.Find.Execute(FindText:="References") Then
        Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
        If rng2.Find.Execute(FindText:="Abbreviations and Acronyms") Then
            ActiveDocument.Range(rng1.End, rng2.Start).Select
            Call Format_AFI_References
        End If
    End If
End Sub
References

Paragraph 1
Paragraph 2
Paragraph 3
etc.,

Abbreviations and Acronyms
Reply With Quote
  #2  
Old 09-08-2018, 04:02 PM
macropod's Avatar
macropod macropod is offline Select pagagraphs between two words and run macro Windows 7 64bit Select pagagraphs between two words and run macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try something along the lines of:
Code:
Sub Find_AFI_Ref()
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "References*Abbreviations and Acronyms"
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Execute
  End With
  If .Find.Found = True Then
    .Start = .Paragraphs.First.Range.End
    .End = .Paragraphs.Last.Range.Start
    Call Format_AFI_References(.Duplicate)
  End If
End Sub
Note how the code passes the relevant range to Format_AFI_References, which you would now code as:
Code:
Sub Format_AFI_References(Rng as Range)
With Rng
…
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-08-2018, 05:25 PM
jeffreybrown jeffreybrown is offline Select pagagraphs between two words and run macro Windows Vista Select pagagraphs between two words and run macro Office 2007
Expert
Select pagagraphs between two words and run macro
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thanks Paul, this works great.

I see how the .Duplicate is what passes the Rng, but I can't figure out or actually understand how the asterisk works in the .Text statement?
Reply With Quote
  #4  
Old 09-08-2018, 08:27 PM
macropod's Avatar
macropod macropod is offline Select pagagraphs between two words and run macro Windows 7 64bit Select pagagraphs between two words and run macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by jeffreybrown View Post
I can't figure out or actually understand how the asterisk works in the .Text statement?
You'll note that the code employs:
.MatchWildcards = True
the asterisk, when used with that, tells Word to find any content beginning with 'References' and ending with 'Abbreviations and Acronyms'. Being a wildcard Find, it's also case-sensitive, which means it won't give false matches with ranges that include either of those in any other case format.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 09-09-2018, 05:31 AM
jeffreybrown jeffreybrown is offline Select pagagraphs between two words and run macro Windows Vista Select pagagraphs between two words and run macro Office 2007
Expert
Select pagagraphs between two words and run macro
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thanks Paul for the additional information. This helps tremendously.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Select pagagraphs between two words and run macro Attempting to find text, select words before it, and format the entire selection KeviM Word VBA 1 04-07-2017 05:36 PM
Select pagagraphs between two words and run macro How to select all incorrect words in Word at once Slavagl Word 2 03-23-2017 11:15 PM
Select pagagraphs between two words and run macro How to select different words at once FTL Word 5 03-08-2016 06:10 PM
Select pagagraphs between two words and run macro Macro for select words? Megan Sprack Word 1 08-23-2012 03:03 PM
Select pagagraphs between two words and run macro Macro to select tables ubns Word VBA 5 04-15-2012 11:44 PM

Other Forums: Access Forums

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