Microsoft Office Forums compare each paragraph with the preceding paragraph

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-07-2015, 09:29 AM
cyraxote's Avatar
cyraxote cyraxote is offline compare each paragraph with the preceding paragraph Windows 7 64bit compare each paragraph with the preceding paragraph Office 2013
Novice
compare each paragraph with the preceding paragraph
 
Join Date: Sep 2015
Location: Greenbelt, MD
Posts: 24
cyraxote is on a distinguished road
Default compare each paragraph with the preceding paragraph

I'm writing a macro to check the style of each paragraph against the previous. (I know that there's a way to do this using the paragraphs object, which apparently has a mysterious "previous paragraph" parameter, but I can't figure it out.) Here's what I have:

Code:
Sub fix2textparas()

Dim Para1, Para2 As String

Selection.HomeKey Unit:=wdStory
Selection.Find.Style = ActiveDocument.Styles("TEXT")
With Selection.Find
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
    .Execute
End With

Do While Selection.Find.Found = True
    Para1 = Selection.Paragraphs(1).Style
    Selection.Next(Unit:=wdParagraph, Count:=1).Select
    Para2 = Selection.Paragraphs(1).Style
    If Para1 = "TEXT" And Para2 = "TEXT" Then
        With Selection.Paragraphs(1)
            .Range.Select
            .Style = "TEXT IND"
            .Range.HighlightColorIndex = wdYellow
        End With
        With Selection.ParagraphFormat
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 6
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
        End With
    End If
    Selection.Collapse Direction:=wdCollapseStart
Loop
End Sub
The problem is that I get an error when I get to the last paragraph and "Selection.Next(Unit:=wdParagraph, Count:=1).Select" tries to move forward. I didn't even want to use this method, but the following methods would not work for reasons I can't even begin to grasp:

Code:
With Selection   
    .Expand Unit:=wdParagraph
End With
Code:
Selection.MoveRight Unit:=wdParagraph, Count:=1, Extend:=wdExtend
The last gives Error 4120 Bad parameter.

I know that I could probably just trap the error, but it seems like there should be a way to do this without having to do so.

The rules for the styles in my document are:
  1. There should never be two consecutive paragraphs with style "TEXT"; if there are, the second should be changed to "TEXT IND."
  2. "TEXT IND" always follows "TEXT"
This means that any 3 paragraphs should never be:

TEXT
TEXT
TEXT IND

or

TEXT IND
TEXT
TEXT IND

The macro only deals with the first of these at the moment, and there are criteria for "TEXT" that are not needed here.

Any help would be great.




Thanks!
Reply With Quote
  #2  
Old 10-07-2015, 03:52 PM
macropod's Avatar
macropod macropod is offline compare each paragraph with the preceding paragraph Windows 7 64bit compare each paragraph with the preceding paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument
  For i = 2 To .Paragraphs.Count
    With .Paragraphs(i)
      If .Range.Text = .Previous.Range.Text Then
        .Previous.Range.HighlightColorIndex = wdYellow
      End If
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 10-07-2015, 03:56 PM
cyraxote's Avatar
cyraxote cyraxote is offline compare each paragraph with the preceding paragraph Windows 7 64bit compare each paragraph with the preceding paragraph Office 2013
Novice
compare each paragraph with the preceding paragraph
 
Join Date: Sep 2015
Location: Greenbelt, MD
Posts: 24
cyraxote is on a distinguished road
Default

Thanks for the response. However, If I'm reading that right, it will compare the text of the paragraph, not the style. Or are you suggesting that I use this as a starting point?
Reply With Quote
  #4  
Old 10-07-2015, 04:33 PM
macropod's Avatar
macropod macropod is offline compare each paragraph with the preceding paragraph Windows 7 64bit compare each paragraph with the preceding paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Which do you want to compare? If you just want to compare Styles, you could use:
If .Range.Style = .Previous.Range.Style Then
If you want to test that paragraphs in a particular Style are preceded by one in another defined Style, you could use code like:
If (.Range.Style = "Style2") And (.Previous.Range.Style = "Style1") Then
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 10-08-2015, 07:44 AM
cyraxote's Avatar
cyraxote cyraxote is offline compare each paragraph with the preceding paragraph Windows 7 64bit compare each paragraph with the preceding paragraph Office 2013
Novice
compare each paragraph with the preceding paragraph
 
Join Date: Sep 2015
Location: Greenbelt, MD
Posts: 24
cyraxote is on a distinguished road
Default

Thanks!

That's so much simpler than what I was trying to do. I wish I had a better grasp of the range object.

So here's what I have:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument
  For i = 2 To .Paragraphs.Count
    With .Paragraphs(i)
      If (.Range.Style = "TEXT") And (.Previous.Range.Style = "TEXT") Then
        .Range.Style = "TEXT IND"
      End If
      If (.Range.Style = "TEXT") And (.Previous.Range.Style = "TEXT IND") Then
        .Range.Style = "TEXT IND"
      End If
    End With
  Next
End With
Selection.WholeStory
    With Selection.ParagraphFormat
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 6
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
    End With
Application.ScreenUpdating = True
End Sub
I can add to this to perform a bunch of checks I've been thinking of. I think Select Case may work better than If... Then for that.

Just one question: why start with the 2nd paragraph?

Thanks again.
Reply With Quote
  #6  
Old 10-08-2015, 04:18 PM
macropod's Avatar
macropod macropod is offline compare each paragraph with the preceding paragraph Windows 7 64bit compare each paragraph with the preceding paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by cyraxote View Post
Just one question: why start with the 2nd paragraph?
Because the 1st paragraph has no previous paragraph to compare with (so there's no point) and trying to do so would cause an error.

I note that your code has:
Code:
Selection.WholeStory
    With Selection.ParagraphFormat
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 6
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
    End With
Do note that's not exactly an optimal way to manage document formatting; what you should do is modify the relevant Styles.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #7  
Old 10-09-2015, 08:03 AM
cyraxote's Avatar
cyraxote cyraxote is offline compare each paragraph with the preceding paragraph Windows 7 64bit compare each paragraph with the preceding paragraph Office 2013
Novice
compare each paragraph with the preceding paragraph
 
Join Date: Sep 2015
Location: Greenbelt, MD
Posts: 24
cyraxote is on a distinguished road
Default

I can't. I don't manage the styles. That is, I'd have to do it for every document. I get the files with the styles already applied, and the default for all styles is double-spacing. I like to edit single-spaced, so I change it and then change it back when I'm done.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing paragraph formatting before column break also changes the next paragraph after the break jjmartin1340 Word 3 09-21-2015 10:50 PM
Split a paragraph bnyamin Word VBA 13 10-05-2014 08:18 PM
compare each paragraph with the preceding paragraph Narrow Paragraph to Wide Paragraph HELP icloudy Word 1 12-09-2012 03:49 PM
spacing within paragraph psmoore Word 1 06-22-2010 02:15 AM
Paragraph cloa513 Word 1 12-29-2009 02:20 AM


All times are GMT -7. The time now is 06:26 PM.


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