reading data from text files in VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Suppose I want to strore certain settings which are used in an add-in in a text file. Is there a way to access the data in this text file directly using VBA?

Thanks in advance,

Arne
 
Arne,

Yes, VBA can directly access text files. See on line help for
the Open, Input, Line Input, Print, and Close statements.
However, it is simpler to save the settings to the registry and
use SaveSetting and GetSetting to write and read the registry
values.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


Arne said:
Suppose I want to strore certain settings which are used in an
add-in in a text file. Is there a way to access the data in this
text file directly using VBA?
 
Arne,

Yes, there is, and it's quite simple also!
Use the Open statement to open your file, e.g.
Open "Path and file name" for Input As #1

Use the Line input statement to get a whole line:
Line Input #1, variable
reads the line text into variable. Next time you execute you get the next
line.

Use Input Function to get a specified number of caharacters:
variable = Input (x, #1)
reads x characters into variable. Next time you execute you get the next x
characters (or a different number).

If you want to read the whole file line by line, use a Do loop with an end
of file condition:
Do Until EOF(1)
Line Input #1, ....
....
Loop

Don't forget to close the file when done!
Close #1

You can open more files simultaneously, just assign different # numbers, and
address by each by its number.

Have a look in VBA help for the statements above, it helps.

HTH,
Nikos

Arne said:
Suppose I want to strore certain settings which are used in an add-in in a
text file. Is there a way to access the data in this text file directly
using VBA?
 
Back
Top