• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

open, read, change a text file

I have a program that I need to open a text file, read the values from it, take those values and do something, change the values and then close it.  Whats the code to open a text file and do those things using VBA?  Thanks a bunch.
  • 2
  • 2
  • 2
  • +1
1 Solution
Dim iNUM as integer
Open "C:\Something" for input as #iNUM

read then

close #iNUM
Hi fbyron

You could try this:

'Put your file you want to use in a variable

Dim yourfile As string

'Then put the name of your file you want to create en use
'in that variable


'for example, if you want to store the content of a
'textbox (text1.text)

Dim contentyoustore As string
contentyoustore = Text1.Text

'Finaly save this content in the file
'If the file doesn't exsist, VB creates it automaticly
' for you

Open yourfile For Output As 1
Print #1, contentyoustore
Close #1

'and now it is saved

'To open an use it again do this:

Open yourfile For Input As 1
Line Input #1, contentyoustore
Close #1

'for example you can put it back in a textbox like this:

Text1.Text = contentyoustore

'If you want to store numeric values I would advice to
' store and get them like this:

Open yourfile For Binary As 1
Put 1, 1, contentyoustore
Close 1

'and to open it again:

Open yourfile For Binary As 1
Get 1, 1, contentyoustore
Close 1

I hope this will help you,
Let me know !!



fbyronAuthor Commented:
If I want to replace a line in a text file what do I do?  I tried print but it just erases everything in the file.
Its reading the file but I want to replace a line that reads Ben, 21 with Ben, 22.  thanks
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

fbyronAuthor Commented:
If I want to replace a line in a text file what do I do?  I tried print but it just erases everything in the file.
Its reading the file but I want to replace a line that reads Ben, 21 with Ben, 22.  thanks
To replace a line in a text file as you use the open,print and close commands, you have 2 options.
1) You save the entire text as one, afterward, you load the entire text in a textbox (with multiple lines) ob better, in a embedded text editor.

But in you case I guess the second option is better,
because its easier to take out the values afterward:

Open yourfile For Binary As 1
Put 1, 1, putyourfirstline
Put 1, 2, putyoursecondline
Put 1, 3, putyourthirdline
' and so on
Close 1

Like this, you should devide your text in different lines,
and store each line in the file your save (1, 2, 3,....)

When you want to change a value or line, you open that file again, get the line you need.  Put it in (for example) a textbox (so the user can change it), ar change the value yourself (in your code)

Good luck
Dim l1 as long, l2 as long
Dim strLine as string
Dim strReplaceLine as string, strNewLine as string
Dim strOriFile as string, strNewFile as string

'set strreplaceline and strnewline before this

'open the file to read from
l1 = freefile()
open strOriFile for input as l1
'open the file to print 2 - if it doesn't exist, this
'will create it
l2 = freefile()
open strNewFile for output as l2

'loop through the first file reading each line
do until eof(l1)
     line input #l1, strLine
     if strReplaceline = strLine then
          print #l2, strLine
          print #l2, strNewLine
     end if

'close both files
close #l1
close #l2

'Remove the original file and rename the new one to it
kill strorifile
name strnewfile as strorifile

There is another way to do this that is much faster as it reads the entire file all at once into a string.  Then you just use the replace command on the string to replace the line in question and then output the entire string to the file essentially overwriting what is alread there.
'With this, you pass in the filename, and it returns the entire file as a string.  See the property get below.

'With the returned string from the property get, just do a replace:
strFileData = replace(strFileData, strReplaceLine, strNewLine)
'Then output it - the open output will clear the entire file:
Dim l1 as long
l1 = freefile()
open strFileName for output as l1
print #l1, strFileData
close #l1

Property Get ReadFileToString(ByVal strFileName As String) As String
    Dim lHandle As long
    ' open in binary mode
    lHandle = FreeFile
    Open strFileName$ For Binary As #lHandle
    ' read the string and close the file
    ReadFileToString = Space$(LOF(lHandle))
    Get #lHandle, , ReadFileToString
    Close #lHandle
End Property

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now