[ACCEPTED]-vbscript : fso.opentextfile permission denied-file-permissions

Accepted answer
Score: 11

I don't think this has to do with File Permissions 14 per se. It has to do with the fact that 13 you've created the file using:

Set objFile = objFSO.CreateTextFile(strDirectory & strFile)

That creates 12 the file...and carries a reference to that 11 file (objFile)

Then you don't close the file 10 before you destroy the reference

...
'Missing objFile.Close here
Set objFile = nothing
Set objFolder = nothing
...

Consequently 9 you're destroying the reference but leaving 8 the textstream open in memory thus locking 7 your file.

You are then proceeding to attempt 6 to re-open the file while the file is already 5 "open". This is a little long winded, you've 4 already got a reference after you've created 3 the file - it would be easier just to write 2 straight to that rather than destroy the 1 reference before creating another one.

Score: 2

for what its worth...

I was convinced I had 7 a permission error because of this line:

Set LogFile = LogFSO.OpenTextFile(LogFileName, ForWriting, True)

Because 6 that's the line that the 'permission denied' error 5 pointed to. But in fact, my permission error 4 was a few lines further down:

WshShell.AppActivate(ScreensToRemove(i))
WshShell.SendKeys ("~")
WScript.Sleep(1000)

There was no 3 screen with such a caption, so the SendKeys 2 is what did not have permission.

The solution, of 1 course, was:

If WshShell.AppActivate(ScreensToRemove(i)) = True Then
   WshShell.SendKeys ("~")
   WScript.Sleep(1000)
End if

Hope that might help.

Score: 1

Also, make sure that you don't have the 2 file open in Excel (I had this problem with 1 a .csv file)...

Score: 1

In my particular case the file which existed 2 before and all I had to do was give permission 1 to the Everyone user

Score: 0

balabaster is exactly right. You either 3 need to close the file before reopening 2 it a second time for writing, or using the 1 existing open handle.

More Related questions