Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open a text file from vb then search and replace the entire text file

Status
Not open for further replies.

eshimano

Programmer
Sep 12, 2000
4
US
How do you open a word text file from within a visual basic program (The location and name will be hard coded).

Then search and replace the entire file.

Then save the changes and close the text file.


Thank's
Esti. [sig][/sig]
 
I cant give you afully working example, but heres a couple sniplets to help you along.

[tt]
Public Function LoadIntoCol(Optional Backup As Boolean = False) As Boolean
[red] Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")

Dim Constants As Object
'Set Constants = CreateObject("Scripting.TextStream")
[/red]


...

If Not fs.FileExists(WebServer & SarasRoot & ConstantFile) Then
MsgBox "An error has occurred while reading from the web server. Make" & _
" sure that the path to the web server is correct and that the web has" & _
" been properly copied.", vbExclamation, "Error Occurred"
LoadIntoCol = False
Exit Function
End If
[red] Set Constants = fs.OpenTextFile(WebServer & SarasRoot & ConstantFile)[/red]
CurrentRowNumber = 1
Do While Not Constants.AtEndOfStream
TextBuffer = Constants.ReadLine
EqualsLocation = InStr(1, TextBuffer, "=")
If EqualsLocation > 0 Then
...
Else
...
End If
...
[red] Constants.Close [/red]
End Function
[/tt]


you can also use the Replace command

[tt]
Replace(TheString, TheTextToFind, ReplaceWith, [optional parameters])
[/tt]

and as for writing to the file (replacing it if it exist)

[tt]
Public Sub WriteCollection(ColType As Integer, Optional filename As String)
[red]
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")

Dim WriteColTxt As Object
[/red]

Dim row As New rows

If ColType = 1 Then
[red] Set WriteColTxt = fs.OpenTextFile(WebServer & SarasRoot & IndexAsp, 2)[/red]
Else
[red] Set WriteColTxt = fs.OpenTextFile(WebServer & SarasRoot & ConstantFile, 2)[/red]
End If

For Each row In WriteCol.rows
...
[red] WriteColTxt.WriteLine row.Variable & "images\" & filename & row.Value [/red]
...
Next

[red]WriteColTxt.Close[/red]
End Sub
[/tt]
[sig]<p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
[/sig]
 
Thanks Karl:

Would you know how to replace &quot;the string&quot; withthe entire text file?

I want it to search the text file from begining to end

Thank's,
E.S.
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top