OpenTextFile - then overwrite it

Z

zSplash

I am having difficulties trying to replace the text of a file using the
"opentextfile" method. Whether I try opening the file with the write
property, or use the alternative "openastextstream" method, I get an error.
Once I've "captured" theText2 (which I want to insert as the text for
x6005.dat file), how do I do that?

Here's my code -- everything works just fine 'til I try to replace my
theText2 into the file.

Sub changeQuotes()
Dim theText As String, theText2 As String
Dim x As Integer, Y As Integer, FSO As Object
Dim myPath As String, myFSO As Object, myText As String
myPath = "c:\windows\x6005.dat"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set myFSO = FSo_Opentextfile(myPath, 1, True)
' Set myFSO = FSo_Openastextstream(myPath, 2)
myText = crthFSO.readall
myFSO.Close
theText = myText
theText2 = Replace(theText, """", "in.")
'now, put this new string in x6005.dat
Set myFSO = FSo_Opentextfile(myPath, 2, True)
myFSO = theText2
myFSO.Close
Set myFSO = FSo_Openastextstream(myPath, 2)
End Function

TIA
 
J

Jezebel

You make life hard for youself using the FSO for this sort of simple file
operation.

Dim pFileNum as long
Dim pText as string
Dim pFileName as string

pFileName = "c:\windows\x6005.dat"\
pFileNum = freefile

open pFileName for input as pFileNum
pText = input(pFileNum, LOF(pFileNum))
Close pFileNum

open pFileName for output as pFileNum
Print pFileNum, Replace$(pText , """", "in.")
Close pFileNum
 
Z

zSplash

Thanks, Jezebel!

Looks like a great idea, Jezebel, but I get errors on these lines:
pText = Input(pFileNum, LOF(pFileNum)) == error "RTE52, Bad file name or
number"
Print pFileNum, Replace$(pText, """", "in.") == error "Method not
available without suitable object"

Is your suggested code MS Scripting, VBA, or something else? (In other
words where can I get more learning on the good direction you always give?)

st.
 
J

Jezebel

Sorry, first one should be

pText = Input(LOF(pfileNum), pFileNum)

Second should be

Print #pFileNum, Replace....
 

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