Split a Document using VBA

P

PaulW

I have a very large word document (2721 pages) which contains a datafile too
large to open in Excel.

My aim is to split this data into manageable chunks for excel to open.

The data source is currently 163,250 lines but will grow daily (unsure by
how much) so I need to put the variable for the loop to be based on the size
of the document.

I have tried to use:

varNumberPages =
ActiveDocument.Content.Information(wdActiveEndAdjustedPageNumber)

but the document cannot repaginate in time to give the correct result.

my main issue is that i cannot say cut the first 1000 pages into a new
document and repeat this another 2 times to get three documents (2 = 1000
pages & 1 = 721) although i am unsure this large a document will open in
excel!

Any help will be gratefully recieved.

PaulW
 
H

Helmut Weber

Hi PaulW,

very likely, a datafile is not a Word-doc at all,
but plain text.

If so, then you just need a programming language
and basic programming skills.
VBA comes in handy. Yes.
Whether it is Word-VBA or Excel-VBA or whatever,
doesn't matter much.

Have a very close look at this sample code:

Sub SplitTxt()
Dim lngLin As Long ' line number
Dim strTmp As String ' string from a line
Dim lngDcm As Long ' number of created doc
Dim strTrg As String ' target path
strTrg = "c:\test\text\"
lngDcm = 2
' ---------------------------------------------
Open "c:\test\thisDoc-001.txt" For Input As #1
Open strTrg & Format(lngDcm, "0000") & ".txt" For Output As #2
While Not EOF(1)
lngLin = lngLin + 1
Input #1, strTmp
Print #2, strTmp
If lngLin Mod 1000 = 0 Then
lngDcm = lngDcm + 1
lngLin = 0
Close #2
Open strTrg & Format(lngDcm, "0000") & ".txt" For Output As #2
End If
Wend

Close #1
Close #2
End Sub

Which could be improved in many ways.

It opens a text-file (source).
Then another file (target), with initial name "0001".
Reads line by line from the source,
whereby it counts the lines,
and writes the lines to the target.
If 1000 lines were written,
then it closes the target file,
and starts counting from 0 again.

It increments the number of the target file by 1,
and does so till the end of the source file was reached.

Not that simple, but basic nevertheless and essential.

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