Sorting paragraphs: is there a length limit?

S

Sverk

Hi, I'm trying to get a Word 2003 macro to sort the paragraphs in a Word
document, using
ActiveDocument.Content.Sort
but it doesn't work when the difference between two paragraphs occurs late
in the paragraphs.
The paragraphs are often 200 characters long or a bit more, and the crucial
difference can occur near the end sometimes.
As I have tested, when the difference occurs within the first 41 character
it works, but not when it occurs later than the 71th character, then they are
treated as identical and don't get sorted.
Gives me a hunch that the Sort procedure has a limit at, say, 64 characters.
Is that so?
If it is, what can I do?
Could the "sort depth" or whatever it may be called be somehow increased?

If not, would something like this be possible:
As it happens, each paragraph always consists of several lines, separated by
new-line characters (shift-return). Assuming each line is within the(?) 64
char limit,
Could the .DOC be treated as a table with each paragraph treated as a record
(row) and the new-line chars as field (cell) separators within the rows?

Please, All help and hints appreciated,
Sverk
 
D

Doug Robbins - Word MVP

You could certainly create a macro that would split each paragraph into a
maximum of 64 character chunks (at the space between words) with each chunk
in a cell of a table, but I guess the problem is to know then on which
column of the table to sort.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Helmut Weber

Hi Sverk,
Gives me a hunch that the Sort procedure has a limit at, say, 64 characters.
Is that so? seems to be.
If it is, what can I do?
Do the sorting yourself, like this:

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
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

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Sverk

Thanks Helmut, I just inserted your code, no changes, and it solved my
problem directly.
This routine, a bubble sort I suppose, isn't very fast, takes about 15
seconds to sort a typical file, but that is fast enough for my present use.
Generally, the VB Help should of course warn about the hidden
paragraph-length limit in the Sort routine -- it has probably been causing
unnoticed sorting errors in my documents for some time, before I recently
noticed an obvious case and started invesigating it.
Even better would be if the limit could be set to higher values, if a length
limit is necessary (for speed reasons?).
Herzliche Grüsse, Danke Schön,
Sverk


"Helmut Weber" skrev:
 
S

Sverk

Oops, I was too quick to say it all works OK with your PrimitiveSort() routine.
It was fast enough when I tested -- but the paragraphs were then already in
almost correct order.
But on the real job, with the same 240 paragraphs in full disorder, each
150-250 chars long, it took several minutes -- and that's very inconvenient.
I found a good solution though: Let the superfast VB's
ActiveDocument.Content.Sort first do the bulk of the job and then run the
PrimitiveSort ()
to take care of the remaining cases -- which typically are few and located
close together. That way I am down to about 10 seconds, and THAT is fully
ok with me.
Vielen Dank, noch einmal,
Sverk


"Helmut Weber" skrev:
 

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