How can I sort paragraphs of text?

C

Chris Gianetto

I'm presently trying to create a macro that will sort paragraphs of text.
Below is an example of one of the paragraphs. I'm trying to sort them with
reference to the "Classification:" number. The number of words between the
header of the paragraph and the classification header is inconsistent. The
end result should maintain the paragraph formatting only altering the
position of the paragraphs placing them into an ascending order.

ITEM ID. URO 001
Sentence of text. Sentence of text:
1. Sentence of text. Sentence of text. Sentence of text. Sentence of text.
*2. Sentence of text. Sentence of text. Sentence of text. Sentence of text.
3. Sentence of text. Sentence of text. Sentence of text. Sentence of text.
4. Sentence of text. Sentence of text. Sentence of text. Sentence of text.
Classification: 08
Keywords: Text. Text Text text.
Ref. Sentence of text. Sentence of text. Sentence of text. Sentence of text.
Sentence of text. Sentence of text. Sentence of text. Sentence of text.
Sentence of text. Sentence of text. Sentence of text. Sentence of text.
Sentence of text.
 
J

John Nurick

I'd export the stuff to a text file; after that it just takes a few
lines of Perl:

$/ = ""; #assumes items are separated by blank lines

my %items;

while (<>) { #read items from file
#add item to hash, with classification as key
$items{$1} = $_ if m/^Classification:\s(\d+)/m ;
}
print "$items{$_}" foreach (sort keys %items);





On Mon, 31 Jul 2006 08:54:01 -0700, Chris Gianetto <Chris
 
C

Chris Gianetto

Thanks for the reply but unfortunately I do not have access to Perl. I'm
trying to create a macro using VBA code that will preform the desired
functions. The macro must sort all paragraphs within a *.doc document into
ascending order. Any ideas for this method woud be greatly appreciated.

Thanks,

Chris Gianetto
 
H

Helmut Weber

Hi Chris,

for sorting paragraphs in alphabetical(!) order,
regardless of formatting:

Sub PrimitiveSort()
Dim i As Long
Dim j As Long
Dim k As Long
Dim aBuf As String
With ActiveDocument.Paragraphs
k = .Count
For j = 1 To k - 1
For i = (j + 1) To (k - 1)
If .Item(i).Range.Text < .Item(j).Range.Text Then
aBuf = .Item(j).Range.Text
.Item(j).Range.Text = .Item(i).Range.Text
.Item(i).Range.Text = aBuf
End If
Next i
Next j
End With
End Sub
The number of words between the header of the paragraph
and the classification header is inconsistent.

Well, I didn't understand all,
but if some input is inconsistent,
then computers get into difficulties in general.

I think I've once posted some code
which takes care of basic formatting, too.

Can't find it anymore.

For sorting, its a science of it's own.
My sample is the most basic possible (bubble sort).

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

John Nurick

Well, you could use the same general approach in Word VBA. Pseudocode:

For each item (your "paragraph" looks as if it contains several
Word paragraphs)
Extract the Classification value (using a wildcard
search or otherwise)
Add the text to a VBScript Dictionary object, using the
Classification value as the key
Next item
Sort the Classification values
For each sorted Classification value
Insert the corresponding item from the Dictionary
Next Classification

Another approach, which may be simpler if you have different formatting
within your items:

Turn your items into a one-column table with one item per row.
Add a column to the table.
For each row, extract the Classification value and place
it in the empty cell (in the new column)
Sort the table on that column (using Word's built-in
sorting facilities)
Delete the column and convert the table back to text.

If your "paragraphs" really are single Word paragraphs, it's simpler:
extract the Classification number and stick a copy of it on the front of
each paragraph, separated with a tab, e.g.

08<tab>ITEM ID. URO 001
Sentence of text....

Then sort the paragraphs using the built-in sort facility; finally use a
wildcard replace to delete the prepended Classification<tab> from each
paragraph.
 
C

Chris Gianetto

Thanks Helmut. Just to clairfy further what my objective is. I'm trying to
sort the paragraphs with reference to a single line of text that contains a
value. Please refer to my initial post as I put a sample paragraph, the
actual document contains 200+ paragraphs thus a macro was the desired tool.
So sorting with reference to a value contained within the paragraph is the
goal.

Thanks,

Chris Gianetto
 
H

Helmut Weber

Hi Chris,
sorting with reference to a value
contained within the paragraph is the goal.

not a problem, leaving formatting aside at first.
but how to locate the value?

If the "value" and the text preceding it,
was typed in by humans, as I assume, you got no chance.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top