Using VB script FileSystemObjects

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
billtubbs
Junior Coder
Posts: 32
Joined: Thu Dec 07, 2006 10:00 pm
Location: Vancouver
Contact:

Using VB script FileSystemObjects

Post by billtubbs » Wed Apr 04, 2007 8:34 pm

I'm trying to access text files in VB using the FileSystemObjects.

Code: Select all

Dim fso, ts
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("C:\Program Files\CIMS\testfile.txt", ForReading)
When I run this code I get the error message shown below. The line corresponds to the third statement above.

Image

This code is in one of the examples posted on the on msdn.microsoft.com:
http://msdn.microsoft.com/library/defau ... 6f4e1d.asp

Other FileSystem objects and methods seem to work fine. Is it something to do with the version of VB I am using perhaps? Has anybody got sample code for reading/writing text files with VB in Macro Scheduler?

Bill.

billtubbs
Junior Coder
Posts: 32
Joined: Thu Dec 07, 2006 10:00 pm
Location: Vancouver
Contact:

Post by billtubbs » Wed Apr 04, 2007 10:28 pm

Sorry! stupid mistake.

I didn't notice that ForReading is a locally defined constant. The code should be:

Code: Select all

Const ForReading = 1
Dim fso, ts 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set ts = fso.OpenTextFile("C:\Program Files\CIMS\testfile.txt", ForReading) 

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts