Previous  Home  Next

Replace one string in a file with another

This demonstrates how to do a simple search and replace on the contents of a file.
The Replace function was introduced in VB6, so this won't work in VB5 or in PowerPoint 97 or any Mac version of PowerPoint (all of which use a VB5-like version of VBA)

Sub ReplaceInFile()

	Dim str As String
	Dim Filenum As Integer
	Dim InputFilename As String
	Dim OutputFilename as String

	Filenum = FreeFile
	InputFilename = "c:\mystuff\input.fil"
	OutputFileName = "c:\mystuff\output.fil"

	Open InputFilename For Binary As Filenum

	' Read the entire contents of the file into the string
	str = Input(LOF(Filenum), #Filenum)
	Close Filenum

	str = Replace(str, "arial", "Arial")

	Open OutputFileName For Binary As Filenum

	Put #Filenum, , str
	Close Filenum

End Sub
Previous  Home  Next