OpenTextFile Method fails


A

Adrian

I'm trying to get a VBA macro to write out a text file.
All my attempts have failed - and worryingly the example
in the online VBA help fails in exactly the same manner.
(fails at the Set f= line with run time error 5, invalid
procedure call or argument.)
Any help welcome.

Sub OpenTextFileTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt",
ForAppending, TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
 
Ad

Advertisements

F

Frank Kabel

Hi Adrian
same results for me. Seems to be the redefinition of ForAppending to 3
(ForAppending should be 8). Try the following (commenting the first
line)
Sub OpenTextFileTest()
'Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt",forappending,
TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
 
Ad

Advertisements

T

Tom Ogilvy

Sub OpenTextFileTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", _
ForAppending, TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
 

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