PDA

View Full Version : [SOLVED:] Macro to split text at a given number of characters



Nebulous
07-03-2016, 06:37 AM
First I'll describe my dream macro, then concessions I can make.The need for this macro arises because I regularly post to a forum that limits posts to 1000 characters. However, the nature of the discussions on the forum lends itself to contributions much larger than that - sometimes 5000 or even 10000 characters. Members get around that by writing "continued" posts. I write my contributions in Word and then manually split them, but that is a pain.


Dream Macro would split the text at no more than 1000 characters, but it would recognize sentence breaks and go shorter if necessary in order to not break sentences. It would continue to do this throughout the entire document. It would indicate the ≤1000 character marks with a visual cue that does not add to the character count, such as changing the last word to a bold, red, underlined font.
The tricky part might be not breaking sentences. If that is an issue, I would still like it to not break words.
If that is an issue, I would still like it to split the text.


In the latter two cases I would need to manually make my adjustments and then re-run the macro.

Help? Thanks!

~N

gmaxey
07-03-2016, 04:56 PM
Something like this perhaps:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc As Document, oDocOut As Document
Dim oRng As Range
Dim oRngPoint As Range
Dim lngEnd As Long
Dim bEnd As Boolean
bEnd = False
Set oDoc = ActiveDocument
Set oDocOut = Documents.Add
oDoc.Activate
Set oRng = oDoc.Range
oRng.Collapse wdCollapseStart
Do
Do
oRng.MoveEnd wdSentence, 1
Loop Until oRng.Characters.Count > 1000 Or oRng.End = oDoc.Range.End
If oRng.End = oDoc.Range.End Then bEnd = True
oRng.MoveEnd wdSentence, -1
Set oRngPoint = oRng.Duplicate
oRngPoint.Collapse wdCollapseEnd
lngEnd = oRngPoint.Sentences(1).Start
lngEnd = lngEnd - 1
oRng.End = lngEnd
oDocOut.Range.InsertAfter oRng.Text & vbCr & vbCr & _
" ****** BREAK at " & oRng.Characters.Count & " *****" & vbCr & vbCr
oRng.Collapse wdCollapseEnd
Loop Until bEnd
lbl_Exit:
Exit Sub
End Sub

Nebulous
07-04-2016, 12:50 PM
Greg - An amazing first pass at solving my problem, and I'm grateful! The counts are not entirely accurate, though, according to Word itself. I'm wondering... is it not counting blank lines such as double blank lines between paragraphs?

I've uploaded an example with Lorem Ipsum text.

The first attachment is the raw document, which MS Word says has 6,008 characters.
The second attachment is the same document, with your macro applied, and two screenshots I added after the macro to show the macro's character count vs. Word's count.


Any idea what is causing the delta?

Thanks!

~N
16552
16553

gmaxey
07-04-2016, 01:50 PM
Neb,

Run this is your Lorem Ipsum document:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
MsgBox "There are " & ActiveDocument.Range.Characters.Count & " characters in this document"
MsgBox "Word says it has " & ActiveDocument.ComputeStatistics(wdStatisticCharactersWithSpaces) & " characters " _
& "and " & ActiveDocument.ComputeStatistics(wdStatisticParagraphs) & " paragraphs." & vbCr + vbCr _
& "That is true, but it also has 1 empty paragrpah. So 6008 + 10 + 1 = 6019"
lbl_Exit:
Exit Sub
End Sub

Nebulous
07-04-2016, 04:11 PM
Greg - So the empty paragraphs are the culprits. Any way those could be accounted for in your original macro, perhaps by counting instances of ^p^p? If not, 'll just manually add them in.

~N

gmaxey
07-04-2016, 06:27 PM
With the exception of the last paragraph it doesn't appear that you have any empty paragraphs. I don't understand the issue. You are not going to be over 1000 characters with the code I gave you.

Nebulous
07-04-2016, 08:06 PM
That was my mistake. I sent you a version that used Word's spacing between paragraphs rather than adding an extra line/empty paragraph. The attached is more representative of what I would be working with.16554

Because the text from Word will ultimately end up in a forum similar to this one, I need to add a blank line between paragraphs. Those evidently don't get counted.

This is very minor, and the macro as it is will solve my problem. Counting blank likes as characters would be helpful, but it is not necessary if it presents a challenge.

Thank you again for all your help Greg!

gmaxey
07-05-2016, 04:03 AM
I still don't understand your concern. In your new attached there are 6028 characters (counting the empty paragraphs) and the code I sent you counts each one of them.

Word reports 6008 because IT does not include the 10 real paragraphs and 10 empty paragraphs.

Nebulous
07-05-2016, 04:44 AM
Now I understand. I was bone headedly thinking Word's **** was authoritative. You're a good man. Tank you sir!
~N

gmaxey
07-05-2016, 08:57 AM
I will consider myself "tanked" ;-). Glad to help.