PDA

View Full Version : [SOLVED:] Dividing a large text file into several text files



mpeterson
05-31-2017, 01:45 PM
Dear All,

I have a large text file with thousands of multiple choice questions as per attached example 19352.
I am in a dire need to divide that file into smaller files, each with 10 questions only.
Original file is located in c:/before_dividing, and desired output is to be placed at c:\after_dividing folder.

Can I be assisted with this request please?

Many thanks in advance..

M.

gmaxey
05-31-2017, 04:47 PM
Something like this should get you started. Be sure to use a copy of you main document as the activedocument:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim lngIndex As Long, lngFile As Long
Dim oRngCut As Range
Dim oDoc As Document
Set oRngCut = ActiveDocument.Range
oRngCut.Collapse wdCollapseStart
Do
Set oRngCut = ActiveDocument.Range
oRngCut.Collapse wdCollapseStart
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Type: "
lngIndex = 0
Do While .Execute
lngIndex = lngIndex + 1
If lngIndex = 9 Then Exit Do
Loop
If lngIndex = 9 Then
oRngCut.End = oRng.End - 6
oRngCut.Cut
lngFile = lngFile + 1
Set oDoc = Documents.Add
oDoc.Range.Paste
oDoc.SaveAs2 "C:\File " & lngFile
oDoc.Close
Else
oRngCut.End = ActiveDocument.Range.End
oRngCut.End = oRng.End - 6
oRngCut.Cut
lngFile = lngFile + 1
Set oDoc = Documents.Add
oDoc.Range.Paste
oDoc.SaveAs2 "C:\File " & lngFile
oDoc.Close
End If
End With
Loop
lbl_Exit:
Exit Sub
End Sub

mpeterson
05-31-2017, 06:47 PM
Dear Greg,

Thank you very much for your continuous, expert assistance that made me really feel indebted of deep gratitude towards your sincere and esteemed person.

As I ran the code, it instantly stopped for a debug with a "Compile error: End With without With" highlighting "End With" above "Loop".

Any suggestions?

Very much appreciated.

M.

gmaxey
05-31-2017, 06:58 PM
See the edit. It should be:

oDoc.Close
End If
End With
Loop

mpeterson
05-31-2017, 07:22 PM
Thank you very very much Greg. It worked like magic. I really don't know what to say; you just saved my day.. Thank you very much..

M.+ Reply to Thread (http://www.vbaexpress.com/forum/newreply.php?p=362341&noquote=1)