Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Macro - reverse lines of text within a selected paragraph

Status
Not open for further replies.

dcorleto

MIS
May 19, 2002
82
US
Please help -

I have the following paragraph in Word

Had last week
Had last 2 weeks
Had last 3 weeks
Had last month

Would like to select the text, run a macro and then have the following result:

Had last month
Had last 3 weeks
Had last 2 weeks
Had last week

(Thereby reversing the order of the selected lines of the paragraph)

Please note that the text is just a sample and will change each time I run the macro - there is no real order to the way the lines within the paragraph are listed. I think I know of a way to do this with a formula in Excel, but would rather not have to take each paragraph out, load it in excel and run formulas, then bring it back to Word - is there a way to do it all within word by using a macro?

Any help is so much appreciated..

DAN
 

Hi,

Try this
Code:
Sub SwitchLines()
    Dim a, i
    a = Split(Selection, Chr(13))
    a = Split(a(0), Chr(11))
    Selection.Delete
    For i = UBound(a) To 0 Step -1
        If i > 0 Then
            Selection.Text = a(i) & Chr(11)
        Else
            Selection.Text = a(i) & Chr(13)
        End If
        Selection.EndOf Unit:=wdCharacter
    Next
End Sub


Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
Thanks for the code, but unfortunately it doesn't work for those items that I select. It does do something, in that one of the line appears, but not the rest of the them...

 


The you are not selecting

1) a PARAGRAPH
2) lines terminated with Chr(11)

which was your criteria

Please post some of the OTHER instances that did not work.

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
Skip -

I know you are right, because there are paragraph marks at the end of each line, which I guess would indicate that they made each line a separate paragraph....(the original document was not created by me...sorry)...Can I send you a snippet of it somehow in Word format so that you can see how they composed it?
 


Please post some of the OTHER instances that did not work.

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
Tough to just paste it in because there are unusual fonts used, etc. Is there a way to place it in here in word format so that you could see all word invisible codes?
 


How do you think that I figgured out the FIRST instance???
[red]
Please post some of the OTHER instances that did not work.
[/red]



Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
L Very favorable
L Somewhat favorable
L Neither favorable or unfavorable
L Somewhat unfavorable
L Very unfavorable
L No opinion
L Never heard of them
L Don’t know

(The "L"'s representa fill in box for a respondent....

Here's some more....


L Yes
L No
L Not sure/Don’t know


L Extremely likely
L Very likely
L Somewhat likely
L Not very likely
L Not likely at all
L Not sure/Don’t know


 


What are you calling a "fill in box"? Please refer to a Word Object.

All 3 worked for me with one exception: this group was not terminated as a paragraph...
[tt]
L Yes
L No
L Not sure/Don’t know
[tt]


Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
The "L" is replaced with a special font that is called "Box" and is installed on my system. It is an open box that sort of looks like a check box.

If I select the text for each group, I only get back one line of text....Perhaps I am performing the macro operation wrong?
 

You must select the entire PARAGRAPH and then run

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
dcorleto,

You have to understand what a paragraph really is to Word.

Also, when you post, it really helps to try and think about giving full information. The fact the sentences have what may be a formfield at the beginning is highly significant. As Skip asked, what is the Word object, the fillin?

For example, here is a little bit different approach to Skip's code. This assumesd

Gerry
 


Try again, Gerry.

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
CRAP! just clicking away like a mad fool.....was trying to flip back to the VBE...

Code:
Sub TestReverse()
Dim oPara As Word.Paragraph
Dim colPara As New Collection
Dim var
For Each oPara In Selection.Paragraphs
   colPara.Add Item:=oPara.Range.Text
Next
Selection.Delete

On Error Resume Next
For var = 0 To colPara.Count
   Selection.TypeText _
      Text:=colPara.Item(colPara.Count - var)
Next
End Sub

Again...this assumes that all paragraphs are terminated by a paragraph mark...which they usually are. So remember this grabs by paragraph and reverses the order.

It does NOT grab formfields! Please confirm what you have. Better yet...please explain why on earth you want to do this?

Gerry
 
Let's try this again.....

Here's the sample...

1. Please tell me what meals you ate today?

r (tab) breakfast[return]
r (tab) lunch[return]
r (tab) dinner[return]

("r" indicates a one letter character such that when I apply a font of wingdings, ir becomes a box. This is not a form, it is for print only. It will be something that a person fills out with a pencil. I do not want a form, as this is eventually just going to be moved out of word and into a real desktop publishing package.

If I select those three lines above, I would like it to return the following:

r (tab) dinner[return]
r (tab) lunch[return]
r (tab) breakfast[return]

Please bear in mind that this may not be limited to three lines, it may be as few as 2, or as long as 30. But they need to be in reverse order. This is because in certain research work, in order to get a good sampling, you have to ask questions in forward and reverse order and then compare your responses. Hope this explantion helps to straighten out the hows and whys of what I am trying to do.

Thanks for all you help and emails in advance..
 


what is not working?

explain, in detail, how you use this macro.

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
I select the lines of text from the far left where the cursor changes to a select whole line mode, and select. The code from fumei works this way, but does not give back the font attributes. This is not a problem. I can do a global find and change either in Word or in the DP program as they are always in the of the format of <paragraph return>L<tab>

Thank you so much - this will really be a huge help - much easier to just globally replace all the "L"'s with the right font, than to manually try to reverse the order of all those lists.
Thankyou Thankyou Thankyou!!!!!
 
Does not give back the font "attributes"? Oh....yeah, it wouldn't, as it is simply picking up the text.

It would not be hard to also pick up fonr info.

but better yet....and those who know me can guess what is coming....USE STYLES! This would solve your font issues.

BTW: <paragraph return>L<tab> is an impossible circumstance in Word. Word does not have a paragraph mark, followed by text, or tab.

Paragraph marks are at the END of, well...paragraphs.

Gerry
 
If I do a find on <paragraph return>L<tab>, it finds them all just fine...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top