Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-14-2021, 02:03 AM
Sark Sark is offline Reverse Paragraph List Macro Windows XP Reverse Paragraph List Macro Office 2003
Novice
Reverse Paragraph List Macro
 
Join Date: Apr 2017
Posts: 25
Sark is on a distinguished road
Default Reverse Paragraph List Macro

Hi all.



I found the macro below to reverses the order of a text (paragraph) list. This works just fine, but affects the entire document.
How could I change this to apply the effect to just selected lines.

I've created a few macros in the past for an old version of CorelDraw I used to use, but the various syntex I've tried don't work with Word. Any advice would be appreciated.

Mark

Code:
Sub ReverseOrderOfParagraphs()
    Dim tmpTable As Table
    Dim rg As Range
   
    Set tmpTable = ActiveDocument.Range.ConvertToTable(Separator:=wdSeparateByParagraphs)
    With tmpTable
        .Columns.Add BeforeColumn:=.Columns(1)
       
        Set rg = .Cell(1, 1).Range
        rg.End = rg.End - 1
        rg.Fields.Add Range:=rg, Type:=wdFieldSequence, Text:="a"
        rg.Cells(1).Range.Copy
       
        .Columns(1).Select
        Selection.Paste
        Selection.Fields.Update
       
        .Sort ExcludeHeader:=False, FieldNumber:=1, SortOrder:=wdSortOrderDescending
       
        .Columns(1).Delete
       
        .ConvertToText
    End With
End Sub
Reply With Quote
  #2  
Old 08-15-2021, 02:26 AM
Guessed's Avatar
Guessed Guessed is offline Reverse Paragraph List Macro Windows 10 Reverse Paragraph List Macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

What happens if you change
ActiveDocument.Range
to
Selection.Range
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-16-2021, 05:50 AM
Sark Sark is offline Reverse Paragraph List Macro Windows XP Reverse Paragraph List Macro Office 2003
Novice
Reverse Paragraph List Macro
 
Join Date: Apr 2017
Posts: 25
Sark is on a distinguished road
Default

That was embarressingly simple.
CorelDraw uses ActiveSelectionRange. I tried a dozen variations of this without success. Thanks for the advice it's much appreciated.

For anyone who comes across this thread in the future, below is the ammended version. I've also included a message box warning when the user forgets to select any text.


Code:
Sub ReverseOrderOfParagraphs()
    Dim tmpTable As Table
    Dim rg As Range

    InSelection = False
    If Selection.Type = wdSelectionIP Then
        MsgBox "Nothing Selected"
   End
End If

    Set tmpTable = Selection.Range.ConvertToTable(Separator:=wdSeparateByParagraphs)
    With tmpTable
        .Columns.Add BeforeColumn:=.Columns(1)
       
        Set rg = .Cell(1, 1).Range
        rg.End = rg.End - 1
        rg.Fields.Add Range:=rg, Type:=wdFieldSequence, Text:="a"
        rg.Cells(1).Range.Copy
       
        .Columns(1).Select
        Selection.Paste
        Selection.Fields.Update
       
        .Sort ExcludeHeader:=False, FieldNumber:=1, SortOrder:=wdSortOrderDescending
        .Columns(1).Delete
        .ConvertToText

    End With
End Sub
Reply With Quote
  #4  
Old 08-16-2021, 04:25 PM
Guessed's Avatar
Guessed Guessed is offline Reverse Paragraph List Macro Windows 10 Reverse Paragraph List Macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

I think the message box at the start needs to be a bit more complex to deal with the likely selections: less than two paragraphs; includes a table
Code:
Sub ReverseOrderOfParagraphs()
  Dim tmpTable As Table, rg As Range
  
  If Selection.Range.Paragraphs.Count < 2 Then
    MsgBox "You must select more than one paragraph"
    Exit Sub
  ElseIf Selection.Range.Tables.Count > 0 Then
    MsgBox "This macro doesn't work with selections which are in tables"
    Exit Sub
  End If
  
  Set tmpTable = Selection.Range.ConvertToTable(Separator:=wdSeparateByParagraphs)
  With tmpTable
    .Columns.Add BeforeColumn:=.Columns(1)
    
    Set rg = .Cell(1, 1).Range
    rg.End = rg.End - 1
    rg.Fields.Add Range:=rg, Type:=wdFieldSequence, Text:="a"
    rg.Cells(1).Range.Copy
    
    .Columns(1).Select
    Selection.Paste
    Selection.Fields.Update
    
    .Sort ExcludeHeader:=False, FieldNumber:=1, SortOrder:=wdSortOrderDescending
    .Columns(1).Delete
    .ConvertToText
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 08-19-2021, 01:41 AM
Sark Sark is offline Reverse Paragraph List Macro Windows XP Reverse Paragraph List Macro Office 2003
Novice
Reverse Paragraph List Macro
 
Join Date: Apr 2017
Posts: 25
Sark is on a distinguished road
Default

Or we could tie it up completely by saying "You must select two or more consecutive paragraphs."

Sark
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How Do I Control Paragraph Spacing in a Numbered List SteveClark590 Word 11 07-08-2021 07:31 PM
How do I order the paragraph styles list? Tunathedog Word 3 04-08-2016 05:37 AM
Reverse Paragraph List Macro Reverse Bold macro brent chadwick Word VBA 15 02-26-2016 05:25 PM
Reverse Paragraph List Macro Is There an Animation that Will Reverse the Order of a Bulleted List equity PowerPoint 3 04-04-2014 12:33 AM
Macro to reverse a test string Jennifer Murphy Word VBA 5 01-16-2014 03:19 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:52 PM.


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