Reverse Sequence of Paragraphs (Word2003)

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Reverse Sequence of Paragraphs (Word2003)

Post by ChrisGreaves »

Here's my Saturday-morning effort after breakfast at http://www.montrealdeli.ca/welcome.htm" onclick="window.open(this.href);return false;, courtesy of my neighbor who bought me a $3.75 breakfast BLT in return for me spending 6 hours fixing her WinXP system three days before she went out and bought a $600 Toshiba Laptop.
(sigh)
The code below is in a module in the attached document.
Theory is you select a set of paragraphs and the macro reverses the sequence.
It seems to work for my purposes, but I know how you guys love picking my code to shreds, so knock yourselves out.
I'm busy playing with my mouse-wheel to see what else it can do.
2.png

Code: Select all

Sub ReverseSequenceParagraphs()
    Dim lngParagraphsCount As Long ' Needed for the last loop, since SELECTION.paragraphs.count will have changed by then
    lngParagraphsCount = Selection.Paragraphs.Count
    Dim rngOldText As Range ' So we can delete the original text at end-of-run
    Set rngOldText = Selection.Range
    Dim rngNewtext As Range ' So we can identify a starting-point for the re-sorted text
    Set rngNewtext = Selection.Range
    rngNewtext.Start = rngNewtext.End ' Dump the re-sorted text one character beyond the original text
    Dim rngAr() As Range ' Holds the paragraph ranges for re-arrangement
    ReDim rngAr(lngParagraphsCount - 1)
    Dim lngAr As Long
    For lngAr = 1 To lngParagraphsCount ' Load the original configuration
        Set rngAr(lngAr - 1) = Selection.Paragraphs(lngAr).Range
    Next lngAr
    For lngAr = 1 To Int(lngParagraphsCount / 2) ' up to, but not including, the mid-way point
        Dim rngSpare As Range ' Swap top-and-bottom, working towards, but not up to, the centre
        Set rngSpare = rngAr(lngAr - 1)
        Set rngAr(lngAr - 1) = rngAr(lngParagraphsCount - lngAr)
        Set rngAr(lngParagraphsCount - lngAr) = rngSpare
    Next lngAr
    rngNewtext.Select ' Prepare to write the new sequence
    For lngAr = 1 To lngParagraphsCount
        Selection.TypeText (rngAr(lngAr - 1))
    Next lngAr
    rngOldText.Delete ' Delete the old sequence
End Sub
You do not have the required permissions to view the files attached to this post.
He who plants a seed, plants life.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Reverse Sequence of Paragraphs (Word2003)

Post by agibsonsw »

If you are just looking to reverse selected paragraphs then I rustled up the following:

Code: Select all

Sub ReversePs()
    Dim pars As Integer, x As Integer
    
    Application.ScreenUpdating = False
    pars = Selection.Paragraphs.Count
    For x = pars - 1 To 1 Step -1
        Selection.InsertAfter Selection.Paragraphs(x).Range.Text
        Selection.Paragraphs(x).Range.Cut
    Next 'x
End Sub
Edited: 'Range.Delete' works in place of 'Range.Cut' as well, and avoids the clipboard.

Another approach would be to number the paragraphs, sort them (Descending) and remove the numbering, but I assume there are many other approaches :cheers:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Reverse Sequence of Paragraphs (Word2003)

Post by agibsonsw »

With some mouse wheels you can push them left or right to scroll horizontally. Never use it myself though :cheers:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Reverse Sequence of Paragraphs (Word2003)

Post by ChrisGreaves »

agibsonsw wrote:I rustled up the following:
And in the process ruffled up my feathers.
Your solution is 40 (text) words whereas mine was 195 words. :clapping: :clapping: :clapping:
Andy, it's people like you that make me want to give up! (grin!)

I think that if you'd been around 50 years ago there would have been no need for Ken Iverson to have dreamed up APL.
And that's saying something.

(Wanders off to cook up some Chewy Oatmeal Bars to console himself; and no, I don't need a shorter recipe ...)

Edited: later ...
He who plants a seed, plants life.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Reverse Sequence of Paragraphs (Word2003)

Post by agibsonsw »

Thank you Chris.

One could even do :grin:

Code: Select all

Sub ReversePs2()
    Dim pars As Integer
    Application.ScreenUpdating = False
    With Selection
        pars = .Paragraphs.Count
        While pars - 1
            .InsertAfter .Paragraphs(pars - 1).Range.Text
            .Paragraphs(pars - 1).Range.Delete
            pars = pars - 1
        Wend
    End With
End Sub
But my work here is done :cheers:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Reverse Sequence of Paragraphs (Word2003)

Post by macropod »

How about:

Code: Select all

Sub Reverse()
Dim StrTxt As String, i As Long
With Selection
  For i = 0 To UBound(Split(.Text, Chr(13))) - 1
    StrTxt = Split(.Text, Chr(13))(i) & vbCr & StrTxt
  Next
  .Text = StrTxt
End With
End Sub
Note: Formatting isn't necessarily preserved.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Reverse Sequence of Paragraphs (Word2003)

Post by ChrisGreaves »

macropod wrote:How about:

Code: Select all

StrTxt = Split(.Text, Chr(13))(i) & vbCr & StrTxt
36 words. :clapping: :clapping: :clapping: :clapping:
You know, Macropod, I never really did like you after I started reading your tome tour de force on {field} calculations. Now my feelings are intensified (HUGE grin!)
Note: Formatting isn't necessarily preserved.
Nor, it seems, is my sanity.

P.S. In case anyone is confused, I am MIGHTILY impressed at Paul's succinctness.
I come from a background of 16 KB computers, with 11 micro-second cycle times, where brevity was critical.
He who plants a seed, plants life.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Reverse Sequence of Paragraphs (Word2003)

Post by agibsonsw »

I think I have a form of OCD and I've continued to play with this..

Is it possible to, using the Find/Replace dialog, replace something with a field? So far I have:
Find: ^p
Replace with: ^p^19 SEQ x^21

where ^19 and ^21 are the field-code braces. But it inserts odd characters, rather than the braces. Apparently, ^d is not permissible in the Replace section, although I can use
^d SEQ x^?
to find this specific field.

I'm just exploring the possibility of inserting a sequence number at the beginning of each paragraph (although the first paragraph is a hiccup), sorting the paragraphs in reverse order, and removing the SEQ fields. My idea is that this would be v. efficient for a huge doc and would preserve formatting. (I realise that this is a detour from the OP.) Andy.

BTW Wandering around the internet I came across these interesting (pointless?) Word shortcuts:
Ctrl-Alt-F1 Display System Information
Ctrl-Alt-u Remove table borders (although it seems to remove all formatting; that is, reverts to the standard 'Table Grid').
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Reverse Sequence of Paragraphs (Word2003)

Post by ChrisGreaves »

agibsonsw wrote:I'm just exploring the possibility of inserting a sequence number at the beginning of each paragraph ... (I realise that this is a detour from the OP.)
Hah!
GOTCHA!
(sort of).
The source document does indeed have sequence fields ({SEQ step}); I "fixed" them in the example so as not to confuse the issue. Or myself.

Background: In preparing a step-by-step procedure I often start out with the goal, and then write down, below it, the objectives I need to reach that goal, thus, in order to restore a Windows partition I need, in reverse sequence:
  • Restore C-Image
  • Make C-Image
  • Boot from USBHDD
  • Build USBHDD
  • Install WAIK
  • Download WAIK
  • Prepare the USBHDD
  • Install Macrium Reflect
  • Download Macrium Reflect
I find it easier to work backwards from the goal.
That's why, when I'm done, I felt the need for a reverse-sequence macro.

P.S. Please-and-Thankyou, what's an OCD when it's at home?
He who plants a seed, plants life.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reverse Sequence of Paragraphs (Word2003)

Post by HansV »

People with OCD would write CDO: the letters have to be in their proper order.

(OCD = Obsessive Compulsive Disorder)
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Reverse Sequence of Paragraphs (Word2003)

Post by agibsonsw »

@Chris: Hi.

If your doc already uses SEQ numbers then you could just highlight the paragraphs and choose Sort from the Table menu (in Word 2003), then Paragraphs and Descending. But.. updating these fields would re-instate the original (increasing) numbering.

(I believe it is/should be possible to modify the SEQ fields so that they run in descending order.. but I only mention this out of interest!)

You are very methodical in your approach, and this is the recommended/sensible approach. That is, to outline the steps required (p- or pseudo-code).

I tend to concentrate on a key element (mini-algorithm) for a problem. That is, starting in the middle - the epicentre if you will :smile: - of a problem. If I can convince myself that my approach will work, and is a good approach, then I fill out the other details. But I'm not a programmer :cheers: (as such..).

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Reverse Sequence of Paragraphs (Word2003)

Post by agibsonsw »

Just to labour the point :grin: if I outline the steps, and convert these to code, I might find that I haven't got a solution for the central issue. So I'll probably end up re-writing, or abandoning, all the code. But if I know that the central issue is solved, then filling out the other details becomes (hopefully) trivial.

For example, with your original question: I knew that I could loop through the paragraphs (forward or backwards), perhaps use an array, perhaps use numbering. But I concentrated on the key question: How do we move a paragraph to a particular place (within the Selection)?
Once I realised that Selection.InsertAfter was a solution, then I abandoned the numbering idea and decided that backwards was the way to go.

But my new issue is that I cannot let it lie! and I've thought about 10 new approaches :hairout: Find/Replace, Regular Expressions, ADO, XML (using XPath or XQuery).

Don't be concerned - I'm just exploring.. :laugh: and have stopped now (apart from my 'replace with field' question(?)). Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Reverse Sequence of Paragraphs (Word2003)

Post by ChrisGreaves »

agibsonsw wrote:If your doc already uses SEQ numbers then you could just highlight the paragraphs and choose Sort from the Table menu (in Word 2003), then Paragraphs and Descending. But.. updating these fields would re-instate the original (increasing) numbering.
Hi Andy. You are (as usual!) quite correct. For my specific task Saturday morning i could have done just that, with the added advantage of preserving the {SEQ} AND having everything neatly re-numbered with a Ctrl-A, F9.
That said, I felt it's be a neat macro to have - re-sequence, physically, whatever was selected regardless of its characteristics; only that it be a set of paragraphs.
I tend to concentrate on a key element (mini-algorithm) for a problem. That is, starting in the middle
I think I do that too, but after looking at it from the outside.
Most of my proof-of-concept videos are just that. When I'm presented with a problem, I specify the goal, then break it down (from the top, downwards), ignoring those components i know I've written in the past (can do) and focusing on any aspect that is novel (never done THAT before).
My proof-of-concept videos then demonstrate that I can tackle the novel aspects.
The result is a planned solution with each component catered for.
He who plants a seed, plants life.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Reverse Sequence of Paragraphs (Word2003)

Post by agibsonsw »

To sign off this post I thought it was interesting to note that it is possible to number in reverse order using field codes:

Code: Select all

{ SET HighNr 11 }{ = HighNr - { SEQ RevNrList } }
{ = HighNr - { SEQ RevNrList } }
{ = HighNr - { SEQ RevNrList } }
.. interesting, but probably not particularly useful :laugh:. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Reverse Sequence of Paragraphs (Word2003)

Post by ChrisGreaves »

agibsonsw wrote: it is possible to number in reverse order using field codes:... interesting, but probably not particularly useful
Hi Andy.
Let me be the judge of that! :grin:
He who plants a seed, plants life.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Reverse Sequence of Paragraphs (Word2003)

Post by macropod »

ChrisGreaves wrote:
macropod wrote:How about:

Code: Select all

StrTxt = Split(.Text, Chr(13))(i) & vbCr & StrTxt
36 words. :clapping: :clapping: :clapping: :clapping:
I could make it shorter, but doing so would make it less efficient ...
Paul Edstein
[Fmr MS MVP - Word]