Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2006

Tip: Looking for answers? Try searching our database.

Using VBA to split a document into sections

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Colin Hayes - 28 Dec 2006 22:47 GMT
Hi

I need to run a small macro to split a passage into sections each
containing 250 characters (including spaces) or less.

Ideally it would split the passage on the page with a couple of carriage
return between each section.

Is this possible?

Grateful for any help.

Best Wishes

Colin
Doug Robbins - Word MVP - 29 Dec 2006 08:01 GMT
Assuming that you want the split to be between words, use

Dim srange As Range
Set srange = ActiveDocument.Range
srange.End = srange.Start + 249
Do While srange.End < ActiveDocument.Range.End
   Do While srange.Characters.Last <> " "
       srange.End = srange.End - 1
   Loop
   srange.InsertAfter vbCr & vbCr
   srange.Start = srange.End + 2
   srange.End = srange.Start + 249
Loop

Signature

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

> Hi
>
[quoted text clipped - 11 lines]
>
> Colin
Colin Hayes - 29 Dec 2006 15:36 GMT
>Assuming that you want the split to be between words, use
>
[quoted text clipped - 9 lines]
>    srange.End = srange.Start + 249
>Loop

Hi Doug

OK thanks very much for that - I'm very grateful. Sorry for posting
twice.

I tried this out and it works a treat. Very neat indeed.

As a small refinement , could it be made to split the text before a
space followed by any number ( 1, 2, 3, etc.)  rather than just before a
space? Splitting before a number followed by a full stop is another
possibility.

This is because my text will always be a numbered listing , so a split
just before the number would be best if possible. This will mean that
the second and subsequent paragraphs will start with the next in the
numbered list , rather than half-way through an item. Can this be done?

I'm grateful for your help.

Best Wishes

Colin
Doug Robbins - Word MVP - 29 Dec 2006 16:33 GMT
Do you want the split before EACH number that is followed by a full stop?
Or, do you want two of more numbered list items together provided that the
total number of characters is such group does not exceed 250?

Signature

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

>>Assuming that you want the split to be between words, use
>>
[quoted text clipped - 30 lines]
>
> Colin
Colin Hayes - 29 Dec 2006 19:14 GMT
>Do you want the split before EACH number that is followed by a full stop?
>Or, do you want two of more numbered list items together provided that the
>total number of characters is such group does not exceed 250?

Hi Doug

No - it would split at the same point in the text that it splits now (as
near to 250 characters as it can get) , but using Space Number as its
cue rather than Space.

It means that the second and subsequent paragraphs will start with a
number.

Here's an example :

1. Original text :

Tracks : 1. Money Money Money (3:08) 2. Honey Honey (2:55) 3. People
Need Love (2:45) 4. He Is Your Brother (3:18) 5. Ring Ring (3:04) 6.
Love Isn't Easy (2:53) 7. So Long (3:05) 8. Eagle (4:27) 9. Summer Night
City (3:36) 10. Angel Eyes (4:19) 11. On And On And On (3:42) 12. When
All Is Said And Done (3:17) 13. Under Attack (3:47)

2. 250 chars split at Space:

Tracks : 1. Money Money Money (3:08) 2. Honey Honey (2:55) 3. People
Need Love (2:45) 4. He Is Your Brother (3:18) 5. Ring Ring (3:04) 6.
Love Isn't Easy (2:53) 7. So Long (3:05) 8. Eagle (4:27) 9. Summer Night
City (3:36) 10. Angel Eyes (4:19) 11.

On And On And On (3:42) 12. When All Is Said And Done (3:17) 13. Under
Attack (3:47)

3. 250 chars split at Space Number

Tracks : 1. Money Money Money (3:08) 2. Honey Honey (2:55) 3. People
Need Love (2:45) 4. He Is Your Brother (3:18) 5. Ring Ring (3:04) 6.
Love Isn't Easy (2:53) 7. So Long (3:05) 8. Eagle (4:27) 9. Summer Night
City (3:36) 10. Angel Eyes (4:19)

11. On And On And On (3:42) 12. When All Is Said And Done (3:17) 13.
Under Attack (3:47)

I think that a split at Space Number would be neater , if it's possible.

Thanks for your help with this.

Best Wishes

Colin
Doug Robbins - Word MVP - 29 Dec 2006 21:55 GMT
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

Signature

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

>>Do you want the split before EACH number that is followed by a full stop?
>>Or, do you want two of more numbered list items together provided that the
[quoted text clipped - 46 lines]
>
> Colin
Colin Hayes - 29 Dec 2006 22:49 GMT
>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
[quoted text clipped - 31 lines]
>    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
Doug Robbins - Word MVP - 30 Dec 2006 10:19 GMT
The mail program has introduced a line break that splits that command
causing the problem

The command

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

should all be on one line or have Visual Basic line break character inserted
in it as

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

Signature

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

>>I would approach that in a different way which the following macro does.
>>It
[quoted text clipped - 61 lines]
>
> Colin
Colin Hayes - 30 Dec 2006 21:28 GMT
>The mail program has introduced a line break that splits that command
>causing the problem
[quoted text clipped - 9 lines]
>       Do While .Paragraphs(j).Range.Characters.Count + .Paragraphs(j + _
>1).Range.Characters.Count < 250

Hi Doug

OK Thanks for that - I'm grateful for you help. I got it to run , and it
works
fine. It does give an error , as you say , but it does the job. Thanks.

BTW Would it be easy to adapt the code below to split a passage at it's
half-way mark?

Dim srange As Range
Set srange = ActiveDocument.Range
srange.End = srange.Start + 249
Do While srange.End < ActiveDocument.Range.End
    Do While srange.Characters.Last <> " "
        srange.End = srange.End - 1
    Loop
    srange.InsertAfter vbCr & vbCr
    srange.Start = srange.End + 2
    srange.End = srange.Start + 249
Loop

Best Wishes

Colin
Doug Robbins - Word MVP - 30 Dec 2006 21:37 GMT
Change the 249 to 224

Signature

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

>>The mail program has introduced a line break that splits that command
>>causing the problem
[quoted text clipped - 35 lines]
>
> Colin
Colin Hayes - 30 Dec 2006 22:48 GMT
>Change the 249 to 224

Hi Doug

Sorry I think I can't have explained correctly.

I meant could the code below be amended to split at the mid-point of any
passage of any length. Sorry if I wasn't clear.

Dim srange As Range
Set srange = ActiveDocument.Range
srange.End = srange.Start + 249
Do While srange.End < ActiveDocument.Range.End
    Do While srange.Characters.Last <> " "
        srange.End = srange.End - 1
    Loop
    srange.InsertAfter vbCr & vbCr
    srange.Start = srange.End + 2
    srange.End = srange.Start + 249
Loop

Best Wishes

Colin
Doug Robbins - Word MVP - 31 Dec 2006 07:06 GMT
Use

Dim srange As Range
Set srange = ActiveDocument.Range
srange.End = srange.Start + srange.Characters.Count / 2
Do While srange.Characters.Last <> " "
    srange.End = srange.End - 1
Loop
srange.InsertAfter vbCr

Signature

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

>>Change the 249 to 224
>
[quoted text clipped - 20 lines]
>
> Colin
Colin Hayes - 31 Dec 2006 19:04 GMT
>Use
>
[quoted text clipped - 5 lines]
>Loop
>srange.InsertAfter vbCr

Hi Doug

OK that works a treat - thanks for all your help. Much appreciated.

Best Wishes

Colin
Shauna Kelly - 30 Dec 2006 10:20 GMT
Hi Colin

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

should all be on one line. The newsgroup post probably broke it
inappropriately.

If you adjust the code and it's no longer red, then you'll know you've
eliminated the syntax error. Then try to run the code again.

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

>>I would approach that in a different way which the following macro does.
>>It
[quoted text clipped - 61 lines]
>
> Colin

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.