Re: Using VBA to split a document into sections



In article <#EfqBQ5KHHA.1240@xxxxxxxxxxxxxxxxxxxx>, Doug Robbins - Word MVP <dkr@xxxxxxxxxxxxxxxxxx> writes
I would approach that in a different way which the following macro does. It
does throw up an error message that you can just ignore. (While I know what
is causing the error, I haven't devised a method to prevent the it from
occuring)

Dim i As Long, j As Long, k As Long
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "( [0-9]{1,}.)"
.Replacement.Text = "^p\1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
i = ActiveDocument.Paragraphs.Count
j = 1
With ActiveDocument
.Paragraphs.First.Range.Characters.Last.Delete
Do While i > 0
Do While .Paragraphs(j).Range.Characters.Count + .Paragraphs(j +
1).Range.Characters.Count < 250
.Paragraphs(j).Range.Characters.Last.Delete
i = i - 1
Loop
j = j + 1
.Paragraphs(j).Range.Characters.First.Delete
Loop
End With




Hi Doug

Thanks very much for that - what a difference a digit makes!

I couldn't get it to run , I'm afraid , with a syntax error given in this line :

Do While .Paragraphs(j).Range.Characters.Count + .Paragraphs(j +
1).Range.Characters.Count < 250

it wouldn't run beyond this unfortunately.

I did wonder if it was a pasting error between this mail program and the VBA screen in Word , and removed a character before the '1' in j + 1. This ran but did give an error as you said , but also seemed to put a return before every number.

This caused a list down the page starting with each track number - clearly not the intention. When I cleared the error popup , the text was returned to original format. Maybe there's another pasting glitch.

Would using the Number Full Stop as a cue be easier VBA-wise than using Space Number , I wonder?



Best Wishes


Colin
.