parsing a text file line by line

L

Lucas Karpiuk

hello,

what approach would i take to creating a macro that parses a text file line
by line? and then each of those lines word by word?

i need to determine for each line and store to variables for later use:
- the number of tabs present
- the width of the words between the tabs

thanks in advance for any help!
 
L

Lucas Karpiuk

after a little playing it occurs to me that this approach isn't going to work
for what i need.

is it possible to run my entire document into a nested array:

so that MyArray[0][0] returns the first token of the first line? (each token
in each line separated by a tab character, and each line separated by a hard
return)

thanks!
 
J

Jonathan West

Which version of Word are you using? It affects what solutions are available
for this.
 
L

Lucas Karpiuk

oh sorry, word 2000

Jonathan West said:
Which version of Word are you using? It affects what solutions are available
for this.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Lucas Karpiuk said:
after a little playing it occurs to me that this approach isn't going to
work
for what i need.

is it possible to run my entire document into a nested array:

so that MyArray[0][0] returns the first token of the first line? (each
token
in each line separated by a tab character, and each line separated by a
hard
return)

thanks!
 
J

Jezebel

Need to be clear on terminology here. A Word document is not a 'text file'
for programming purposes.

There's no easy way to parse a Word document line-by-line unless each line
ends with a paragraph mark -- in whcih case your 'lines' are actually
paragraphs:

Dim pPar as Word.Paragraph
Dim pWord as Word.Range

For each pPar in ActiveDocument.Paragraphs
...
For each pWord in pPar.Words
...
Next
Next


Lucas Karpiuk said:
oh sorry, word 2000

Jonathan West said:
Which version of Word are you using? It affects what solutions are available
for this.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Lucas Karpiuk said:
after a little playing it occurs to me that this approach isn't going to
work
for what i need.

is it possible to run my entire document into a nested array:

so that MyArray[0][0] returns the first token of the first line? (each
token
in each line separated by a tab character, and each line separated by a
hard
return)

thanks!
 
J

Jonathan West

OK, provided that your file really is a text file, the following
demonstrates how to parse it and how to use the resulting array.

Dim vText As Variant
Dim vArray() As Variant
Dim i As Long
Dim j As Long

Open "C:\My Documents\testfile.txt" For Input As #1
vText = Input(LOF(1), #1)
vText = Split(vText, vbCrLf)
ReDim vArray(UBound(vText))
For i = 0 To UBound(vText)
Debug.Print vText(i)
vArray(i) = Split(CStr(vText(i)), vbTab)
Next i

For i = 0 To UBound(vText)
For j = 0 To UBound(vArray(i))
Debug.Print i; j; vArray(i)(j)
Next j
Next i

This will work for any version of Word from Word 2000 onwards. Word 97 lacks
the Split command.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Lucas Karpiuk said:
oh sorry, word 2000

Jonathan West said:
Which version of Word are you using? It affects what solutions are
available
for this.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Lucas Karpiuk said:
after a little playing it occurs to me that this approach isn't going
to
work
for what i need.

is it possible to run my entire document into a nested array:

so that MyArray[0][0] returns the first token of the first line? (each
token
in each line separated by a tab character, and each line separated by a
hard
return)

thanks!
 

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