insert part of a text file

N

nik

Hi all,

I have got a big text file (64MB) and want to insert into word document the
first 20,000 lines only of the text.

Could anybody help me?

Regards,

Nikos
 
H

Helmut Weber

Hi Nikos,

things get quite another quality,
when processing files of such size.

I'd leave Word aside first and
put the first 20,000 lines of the source file
into another file before using Word at all,
like this:

Sub M389()
Dim sTmp As String
Dim lCnt As Long
Open "c:\test\text\largefile.txt" For Input As #1
Open "c:\test\text\20000.txt" For Output As #2
For lCnt = 1 To 20000
Line Input #1, sTmp
Print #2, sTmp
Next
Close #1
Close #2
End Sub

Still difficult enough, when using Word later,
to stop it from checking spelling and repaginating.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

nik

That was great!! thank you very much.
I used excel for that macro and then imported it into word.

Now, I have got another text file where I would like to get a specific
amount of lines but I want to specify the starting point and length.

I would be much grateful.

Regards,

NIkos
 
H

Helmut Weber

Hi Nikos,
That was great!! thank you very much.
feels good to read.
I used excel for that macro and then imported it into word.
You don't need Excel.
The code should work from any Office-application.
Now, I have got another text file where I would like to get a specific
amount of lines but I want to specify the starting point and length.

Like this, which I did not test a million times.
You have to take care yourself of attempted reading
over the end of file or of invalid arguments passed
to ReadFromTextFile.
Error handling is usually more difficult than
putting up some code that works in principle.

' ----------------------------
Sub ReadFromTextFile(lStart As Long, lNumb As Long)
Dim sTmp As String ' a temporary string
Dim lTmp As Long ' a temporary long
Open "c:\test\text\largefile.txt" For Input As #1
Open "c:\test\text\smallfile.txt" For Output As #2
For lTmp = 1 To lStart - 1
Line Input #1, sTmp
Next
For lTmp = lStart To lNumb
Line Input #1, sTmp
Print #2, sTmp
Next
Close #1
Close #2
End Sub
' ----------------------------
Sub TestRead()
Call ReadFromTextFile(10, 90)
Documents.Open "c:\test\text\smallfile.txt"
End Sub

Note that with arguments 10, 90 you get 91 paragraphs.
A minus 1 somewhere in the code might solve that.
Or use 89 if you want to get 90 paragraphs. :)


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

nik

Hi Helmut,

I did the follow:-

Sub ReadFromTextFile(lStart As Long, lNumb As Long)
Dim sTmp As String ' a temporary string
Dim lTmp As Long ' a temporary long
Open "c:\test\output.txt" For Input As #1
Open "c:\test\smallfile.txt" For Output As #2
For lTmp = 1 To lStart - 1
Line Input #1, sTmp
Next
For lTmp = lStart To lNumb
Line Input #1, sTmp
Print #2, sTmp
Next
Close #1
Close #2
End Sub
------------------------------------
Sub ReadFromTextFile1(lStart As Long, lNumb As Long)
Dim sTmp As String ' a temporary string
Dim lTmp As Long ' a temporary long
Open "c:\test\output.txt" For Input As #1
Open "c:\test\smallfile1.txt" For Output As #2
For lTmp = 1 To lStart - 1
Line Input #1, sTmp
Next
For lTmp = lStart To lNumb
Line Input #1, sTmp
Print #2, sTmp
Next
Close #1
Close #2
End Sub
------------------------
Sub TestRead()
Call ReadFromTextFile(1, 90)
Call ReadFromTextFile1(91, 200)
End Sub

It is working fine!! I will double check it later but up to now it is seems
ok to me.

You are a star!!

thank you!!
 

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