Using Selection is generally a not good way of processing. It is better to use Range.
The following may, or may not, work properly.
1. It depends on what you happening qwith your document. If you are using Styles, and NOT using "extra' paragraph marks to make space between paragraphs, this will have to be adjusted.
2. Bordered paragraphs that touch (are contiguous) will merge borders. This adds a NON-bordered paragraph between the bordered ones.
Here is the starting text. The <p> indicates a paragraph mark. I am also using TGML format to indicate the whole "chunks". Chunk 1 is italics, Chunk is plain, Chunk 3 is bold.
# Fri 11 May Subject: Formatting Test<p>
This is a test of automatically formatting the extracted text<p>
and more testing.<p>
<p>
This is a test of automatically formatting the extracted text<p>
and more testing.<p>
<p>
# Sat 12 May Subject: Still formatting text<p>
This is the second section.<p>
Still the second section<p>
This is the second section.<p>
Still the second section<p>
<p>
# Sun 13 May Subject: 3rd section<p>
Blah blah<p>
Blah blah blah.<p>
The code will put a border around each "chunk", adding a separator paragraph in order to stop the borders from merging.
Code:
Sub FindChunks()
Dim r As Range
Dim CsetReturn As Long
Set r = ActiveDocument.Range(Start:=0, End:=0)
Do Until r.End = ActiveDocument.Range.End
With r
CsetReturn = _
(.MoveEndUntil(Cset:="#", Count:=wdForward))
If CsetReturn >= 1 Then
.Collapse Direction:=wdCollapseEnd
.MoveEnd unit:=wdCharacter, Count:=1
CsetReturn = _
(.MoveEndUntil(Cset:="#", Count:=wdForward))
If CsetReturn >= 1 Then
Call ApplyBorders(r)
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
Else
.End = ActiveDocument.Range.End
Call ApplyBorders(r)
End If
Else
.MoveStart unit:=wdCharacter, Count:=1
.End = ActiveDocument.Range.End
Call ApplyBorders(r)
End If
End With
Loop
End Sub
Sub ApplyBorders(r As Range)
Dim var
With r.ParagraphFormat
For var = -4 To -1
With .Borders(var)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
Next
End With
End Sub
Note the change to ApplyBorders. In the recorded macro there are all those With...End With instructions for each of the borders. wdBorderTop, wdBorderLeft, wdBorderRght, dBorderBottom
In this case, each range is passed to ApplyBorders as a parameter, and the format is applied to it.
This is one of the things with recorded macros. They often add huge amounts of clutter. By using the numeric values of wdBorderTop etc. you can process them all in a simple loop.
Essentially what is happening is a range object is created at the start of the document. It moves the End until it finds a "#". It collapses, moves forward to include the "#", then moves forward to find the next "#".
If it finds one - CsetReturn = 1 or greater - it applys the border.
If it does not - it makes the Range from the currently found "#", to the end of the document. Then applies the border.
Of course this most likely may have to be adjusted, as that final assuption could be incorrect. It was not stated if the
last "#" does indeed include up to the end of the document.
No Selection is made.
Gerry
My paintings and sculpture