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 / July 2007

Tip: Looking for answers? Try searching our database.

Split a Document using VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PaulW - 26 Jul 2007 12:42 GMT
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
Helmut Weber - 26 Jul 2007 16:33 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
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.