re-order a color-coded document

G

Giovanni

Hello everybody,
I have a huge Word document with mixed color-coded sections (usually
paragraphs). I'd like to have a macro to re-order the document based on
the colors (for example: all the red paragraphs, followed by the greens
etc.).
I am thinking to a macro starting from the beginning, cutting each
paragraph, evaluating it for the color, and pasting it at the end of
the document in a specific section.
Can anybody point me in the right direction? (Existing macros, commands
to evaluate the format etc.)?
I'd also like to put the document back like it was, but I don't know
how to do it (especially after modifications...).
Thanks in advance for your help
Giovanni
 
H

Helmut Weber

Hi Giovanni,

far more complicated than I thought.
I have a huge Word document with mixed color-coded sections (usually
paragraphs).

"usually" is a real problem

Just to get you going, with many mays of improvements left,
and I wonder whether someone comes up with a perfect solution:


Sub PutAllRedAtEndofDoc()
Dim lCnt As Long
Dim ltmp As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Font.Color = wdColorRed
While .Execute
lCnt = lCnt + 1
Wend
End With
For ltmp = 1 To lCnt
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Font.Color = wdColorRed
If .Execute Then
rDcm.Select
Selection.Copy
Selection.Delete
Selection.EndKey unit:=wdStory
Selection.TypeParagraph
Selection.Paste
End If
End With
Next

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Giovanni

Hello Helmut,
thank you very much for your reply. A different way to address this
would be to assign bookmarks to paragraphs, and then do the sorting
based on that. This would help in sorting back the document after
paragraph modification, because the paragraphs would retain the
original bookmark... Does this seem more feasible? I just don't know if
you can organize bookmarks 'by family' and sort a document based on
bookmarks....
Thanks again for your time
Giovanni
 
H

Helmut Weber

Hi Giovanni,

if you want to redo everything,
kind of,
just hold a copy a the original document.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Dave Lett

Hi Giovanni,

You can use something like the following, as a starting point:

Dim oRngEnd As Range
Dim oRng As Range
Set oRngEnd = ActiveDocument.Range
oRngEnd.Collapse Direction:=wdCollapseEnd
Set oRng = ActiveDocument.Range
oRng.Collapse Direction:=wdCollapseEnd
With Selection
.EndKey Unit:=wdStory, Extend:=wdMove
With .Find
.Wrap = wdFindStop
.Forward = False
.Text = ""
.Font.Color = wdColorRed
Do While .Execute
oRng.FormattedText = Selection.FormattedText
oRng.Collapse Direction:=wdCollapseStart
Selection.Delete
Loop
End With
End With

HTH,
Dave
 
D

Dave Lett

Hi Giovanni,

This one might be a little more useful. You can set up an array of colors to
search for and set the order of a number of different colors (i.e., more than
two colors) in this array.

Dim aColors
Dim iCount As Integer
aColors = Array(wdColorRed, wdColorAutomatic, wdColorGreen)
For iCount = 0 To UBound(aColors)
Set oRng = ActiveDocument.Range
oRng.Collapse Direction:=wdCollapseEnd
With Selection
.EndKey Unit:=wdStory, Extend:=wdMove
With .Find
.Wrap = wdFindStop
.Forward = False
.Text = ""
.Font.Color = aColors(iCount)
Do While .Execute
With oRng
.FormattedText = Selection.FormattedText
If .Characters(.Characters.Count) <> Chr(13) Then
.InsertAfter vbCrLf
End If
.Collapse Direction:=wdCollapseStart
End With
Selection.Delete
Loop
End With
End With
Next iCount

HTH,
Dave
 
H

Helmut Weber

Hi Giovanni
A different way to address this would be to assign bookmarks
to paragraphs, and then do the sorting based on that.

hm...

of course you can add bookmarks like "green01", "green02"
to all pieces of green text, and process bookmarks of kind
"green" like "red" like "blue".
But I doubt, whether this makes things easier.

And then you spoke about _usually_ paragraphs,
which means you have to process characters, words and all.

By the way, there are so often conditions, one can hardly
think of beforehand. As you can't include the last paragraph
mark in a doc in a bookmark.

And I forgot about the most important question:
What do you want to achieve?

Maybe a quite different way would be much better,
like collecting all colored text in separate documents,
or formatting all text with font.color.automatic as hidden
or...
 
G

Giovanni

Hi Helmut,
I actually think that bookmarks could be better, because I assume they
would allow me to put the document back like it was even after
paragraph modifications. Do you know how to sort a document based on
bookmarks?
I found this bookmark manager, I'll check it out:
http://gregmaxey.mvps.org/Bookmark_Tool.htm.
The document is just a diary I use to follow up on several projects.
There is something about each of them every day, and I color-coded the
paragraphs to quickly recognize the project they are referred to. So,
my goal is to have a button to toggle between 'day-view' and 'project
view', allowing me to modify paragraphs in both views...
I can limit to paragraphs, and replace 'usually' with 'always' if this
makes things easier.
What do you mean by 'last paragraph mark in a doc in a bookmark'?
I really appreciate your help
Best
Giovanni
 
H

Helmut Weber

Hi Giovanni,

all far too complicated, I think.
But you can sort the whole doc or only the selection
in a variaty of ways, if you can stick to a numbering schema,
like 1 <delimiter> 2 <delimiter> 3, at a paragraphs start.
In the table-sort-dialog you can then chose whether
you want to sort according to field1 or field2 or field3, e.g.

Your paragraphs would look like:
01|04|07: some text some text some text some text
02|03|05: some text some text some text some text

Whereby the first field could hold the day,
the second the project, the third the project step.

All you got to do is to choose the delimiter wisely, IMHO.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Giovanni

I think I found a way:
1) at the start, the macro numbers all the paragraphs
2) then it orders them based on the color, using your code or similar
modified to evaluate the date header
3) after modifications to the paragraphs, I can have the order back
based on the numbering!
I might need to convert the numbers to regular text before making
changes, but I think it can work... what do you think?
Thank you so much for your help
Giovanni
 
H

Helmut Weber

Hi Giovanni,
I might need to convert the numbers to regular text before making
changes, but I think it can work... what do you think?

I wouldn't use automatic numbering at all,
but do it all by myself.

Ciao

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