ReadFile

K

Kevin

I am attempting to read information saved previously in a text file. I get a
permission denied error which leads me to believe the process which last
wrote information to the file still has control of the file. As far as I can
tell this process finished normally. That previous process wrote information
to the file and the information I expect has been written to the file, but it
is apparently not releasing the file handle. The module which writes
information to the file is as follows:

Public Function WriteToFile(FName As Variant, OWrite As Boolean, myWriteData
As Variant
'**********************************************************************************************
'Author:
'Date:
'Purpose: To create a file using the path and file name passed into the
routine using the fName variable.
'FName represents the filename to create which is to include the full path
to the file.
'OWrite is a boolean to indicate if the file is to be overwritten
'myWriteDate represents the data to be written to the file
'*******************************************************************

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s As Variant

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateTextFile(FName, OWrite)

Set f = fs.GetFile(FName)
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write myWriteData
ts.Close
Set ts = Nothing
Set f = Nothing
Set fs = Nothing

End Function
 
G

GJones

Hi Kevin;

It appear that the routine is requiring one paramaeter
that is not being used:

myWriteData

That might be causing the share violation.

Thanks,

Greg
 
S

sebastienm

Hi Kevin,
I also received the 'permission' error wioth your code.
I believe you were creating the file through f in the file creation without
closing it before reassigning f a second time to a file (here, the same
file).
The following code seems to work for me.
Regards,
Sebastien
'--------------------------------------------------------------
Public Function WriteToFile(FName As Variant, OWrite As Boolean, myWriteData
As Variant)

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs As FileSystemObject, f As File, ts As TextStream, s As Variant
Dim n

Set fs = CreateObject("Scripting.FileSystemObject")

'Create the file
Set ts = fs.CreateTextFile(FName, OWrite)
ts.Close

'Process the file.
Set f = fs.GetFile(FName)
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write myWriteData
ts.Close
Set ts = Nothing
Set f = Nothing
Set fs = Nothing

End Function
'----------------------------------------------------------
 
K

Kevin

Sebastien,

Thanks for responding!

I get a compile error in the following:

Dim fs As FileSystemObject, f As File, ts As TextStream, s As Variant

There error is occuring on the FileSystemObject, File, and TextStream data
types. It is considering them user defined datatypes.

?? Am I missing a Reference? Something else?

I copied and pasted your code into the VBA editor and received the error
when I tried to run the code. I changed the datatypes to variant and was able
to proceed, but am still getting the original error.

Kevin.
 
S

sebastienm

ooops you're right, i forgot to mention i added a reference (menu Tools >
References) to the Microsoft Scripting RuntTime libraery.

Sebastienm
 

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