Importing a non-delimited text file into table

D

dave.cuthill

I have a text file that has a constant number of characters per line
and these characters can be spaces or some form of meaningful text that
appears to be a table of columnar data when viewed in notepad. I am
trying to import this data though vba into a word table. I know that
the first 3 characters are going to be the data for column 1,row 1 and
the next 5 characters will be the data for column 2, row 2 , and on and
on until I reach the end of the line.

How do a step through the file line by line in order to get the data
into the table??


David
 
J

Jezebel

Dim pRows() as string
Dim pFileNum as long
Dim pTable as Word.Table
Dim pIndex as long

1. Read the file into an array --
pFileNum = freefile
Open MyFile for input as pFileNum
pRows = split(input(lof(pfilenum),pfilenum), vbrcr) 'you might
need to use vbcrlf
close pfilenum

2. Create the table
set pTable = activedocument.tables.add(Range:=[wherever it goes],
NumRows:=ubound(pRows), NumColumns:=...)

3. Populate the table
for pIndex = 1 to ubound(pRows)
pTable.Cell(pIndex,1).range = trim(mid(pRows(pIndex), 1, 3))
pTable.Cell(pIndex,2).range = trim(mid(pRows(pIndex), 4, 5))
:
Next
 
D

dave.cuthill

Thanks this works great.

How do I populate the file name interactively? Can I use
Dialogs(wdDialogFileOpen).Show or the equivalent of Getfilename??


David

Dim pRows() as string
Dim pFileNum as long
Dim pTable as Word.Table
Dim pIndex as long

1. Read the file into an array --
pFileNum = freefile
Open MyFile for input as pFileNum
pRows = split(input(lof(pfilenum),pfilenum), vbrcr) 'you might
need to use vbcrlf
close pfilenum

2. Create the table
set pTable = activedocument.tables.add(Range:=[wherever it goes],
NumRows:=ubound(pRows), NumColumns:=...)

3. Populate the table
for pIndex = 1 to ubound(pRows)
pTable.Cell(pIndex,1).range = trim(mid(pRows(pIndex), 1, 3))
pTable.Cell(pIndex,2).range = trim(mid(pRows(pIndex), 4, 5))
:
Next



I have a text file that has a constant number of characters per line
and these characters can be spaces or some form of meaningful text that
appears to be a table of columnar data when viewed in notepad. I am
trying to import this data though vba into a word table. I know that
the first 3 characters are going to be the data for column 1,row 1 and
the next 5 characters will be the data for column 2, row 2 , and on and
on until I reach the end of the line.

How do a step through the file line by line in order to get the data
into the table??


David
 
J

Jezebel

You can use the Dialogs().Display method to use the dialog without executing
it -- ie to get a filename but without Word opening the file. Or you can
load the CommonDialog control onto a form and use that -- you don't need to
display the form to call the dialog. Or if it's just for your own use, you
could simply use an inputbox().




Thanks this works great.

How do I populate the file name interactively? Can I use
Dialogs(wdDialogFileOpen).Show or the equivalent of Getfilename??


David

Dim pRows() as string
Dim pFileNum as long
Dim pTable as Word.Table
Dim pIndex as long

1. Read the file into an array --
pFileNum = freefile
Open MyFile for input as pFileNum
pRows = split(input(lof(pfilenum),pfilenum), vbrcr) 'you might
need to use vbcrlf
close pfilenum

2. Create the table
set pTable = activedocument.tables.add(Range:=[wherever it goes],
NumRows:=ubound(pRows), NumColumns:=...)

3. Populate the table
for pIndex = 1 to ubound(pRows)
pTable.Cell(pIndex,1).range = trim(mid(pRows(pIndex), 1, 3))
pTable.Cell(pIndex,2).range = trim(mid(pRows(pIndex), 4, 5))
:
Next



I have a text file that has a constant number of characters per line
and these characters can be spaces or some form of meaningful text that
appears to be a table of columnar data when viewed in notepad. I am
trying to import this data though vba into a word table. I know that
the first 3 characters are going to be the data for column 1,row 1 and
the next 5 characters will be the data for column 2, row 2 , and on and
on until I reach the end of the line.

How do a step through the file line by line in order to get the data
into the table??


David
 

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