Macro to count 250 characters with spaces and insert comma

J

JohnGIS

I have never posted to this community but, I am in desperate need of a macro
to automate some lengthy tasks.

I use a GIS software that utilizes .dbf files that correspond to
geographical features. The .dbf file has a limit of 254 characters per
field. I have MS Word documents in which I need to automatically seperate
each paragraph into a "250 character with spaces" lines seperated by inserted
commas throughout each paragraph. This way I could import this delimited
file as a .csv into a .dbf

To explain in another short way, is there a macro that will look at each
paragraph and separate each paragraph into (250 character with spaces)
segments delimited by a comma per paragraph.

Thank you for your time.

John
 
H

Helmut Weber

Hi John,

like this, but I wonder whether you'd like the result,
as it would cut all paragraphs in 250 character long pieces,
no matter where that cut would be.
Could be in the middle of a number or of a word.

Sub test90a()
Dim l As Long ' 250 * factor
Dim k As Long ' offset
Dim sTmp As String

Dim oPrg As Paragraph
sTmp = chr(34) & "," & chr(34)
For Each oPrg In ActiveDocument.Paragraphs
k = 0
l = 1
While Len(oPrg.Range) > 250 * l + k
oPrg.Range.Characters(250 * l + k).InsertAfter sTmp
k = k + 3 ' increment offset
l = l + 1 ' increment factor
Wend
Next
For Each oPrg In ActiveDocument.Paragraphs
oPrg.Range.InsertBefore chr(34)
oPrg.Range.Characters.Last.InsertBefore chr(34)
Next
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

JohnGIS

Mr. Weber,

Thank you very much for your immediate response! Works like a charm!

Yes, it does cut words in half but anything beats having to highlight text
and click character count each and every time by trial and error.

Mabey when I get some time I will incorporate a conditional statement to
look for tha end of a word. If you have any suggestions on this I would
appreciate it. Otherwise, thanks again for your help. I am currently
getting my "feet wet" in VB programming.

Regards,

John
 
H

Helmut Weber

Hi John,

don't look for words.
Look for the last space before character 250.
or have a look at split() and join().
and split the whole paragraph with a space as delimiter.
Add the items in the array until the added length plus
the delimiters exceeds 250. Join all but the last item,
which would cause the string to be longer than 250.
And, and and...

Lots of not difficult, but tedious coding.

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