Runtime error when using FSO to create a text file in VBA

melodic_agony

New Member
Joined
Aug 30, 2017
Messages
2
I'm using File System Object to create a text file but when I run the code I'm getting a runtime error that I don't recognize. Code works fine if the text file exists and it just executes and overwrites the existing file.

The code is:

Sub main()
Dim fso As FileSystemObject
Dim Fileout As Object
...


'Create log file.
Set fso = CreateObject("Scripting.FileSystemObject")
Set Fileout = fso.CreateTextFile("c:\ConfigErrorLog.txt", True)

When run (without the file already existing), VBA throws:

"Run-time error '-2147023582... etc'
Method 'CreateTextFile' of object 'IFileSystem3' failed"

and sits on that last line of code.

Does anyone know what could cause this?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
It could be a user permission issue.
Can you create a text file in C:\ manually from Windows Explorer? Do you get same error for other directories too?
 
Upvote 0
It could be a user permission issue.
Can you create a text file in C:\ manually from Windows Explorer? Do you get same error for other directories too?

Yes I can create one manually and it can be over written by the code. However the code cannot create a new file.

I think I can use the Environs method to pull a path and maybe create the file in c:\~USER\Documents instead of c:\. I need practice working with the file system! Too much time writing macro apps...
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,420
Members
452,325
Latest member
BlahQz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top