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 / November 2004

Tip: Looking for answers? Try searching our database.

parsing a text file line by line

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lucas Karpiuk - 08 Nov 2004 17:43 GMT
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!
Lucas Karpiuk - 08 Nov 2004 18:34 GMT
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!
Jonathan West - 08 Nov 2004 19:22 GMT
Which version of Word are you using? It affects what solutions are available
for this.

Signature

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

> after a little playing it occurs to me that this approach isn't going to
> work
[quoted text clipped - 9 lines]
>
> thanks!
Lucas Karpiuk - 08 Nov 2004 20:39 GMT
oh sorry, word 2000

> Which version of Word are you using? It affects what solutions are available
> for this.
[quoted text clipped - 12 lines]
> >
> > thanks!
Jezebel - 08 Nov 2004 20:52 GMT
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

> oh sorry, word 2000
>
[quoted text clipped - 14 lines]
> > >
> > > thanks!
Jonathan West - 08 Nov 2004 21:13 GMT
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.

Signature

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

> oh sorry, word 2000
>
[quoted text clipped - 16 lines]
>> >
>> > thanks!
 
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.