Why is my variable assignment not working?

A

Ayo

Sub ShowFileAccessInfo(filespec)
Dim fs, f, fn As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
fn = f.Name
CheckFileNames fn
End Sub

f.Name is showing " Summary.xls" but fn is showing "'. I can't figure this
out. This same snippet of code works fine in other macro that I used it in.
Is there something i am doing wrong but can't see'
Help!!!
 
J

Joel

You need to add the path name to the filespec. Get file returns an empty
string if it doesn't find the file.

He is an example of creating a new file

Sub TextStreamTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateTextFile "test1.txt" 'Create a file
Set f = fs.GetFile("test1.txt")
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write "Hello World"
ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
s = ts.ReadLine
MsgBox s
ts.Close
End Sub

And here is an example of using a path name witth a scripting object

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
 
M

Michael

Try adding a function that will return the name:
Function GetAName(DriveSpec)

Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")

GetAName = fso.GetFileName(DriveSpec)

End Function

Then your routine:

Sub ShowFileAccessInfo(filespec)
Dim fs, f, fn As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
fn = GetAName(filespec)
CheckFileNames fn
End Sub
 
A

Ayo

Thanks Joel I will try this.

Joel said:
You need to add the path name to the filespec. Get file returns an empty
string if it doesn't find the file.

He is an example of creating a new file

Sub TextStreamTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateTextFile "test1.txt" 'Create a file
Set f = fs.GetFile("test1.txt")
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write "Hello World"
ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
s = ts.ReadLine
MsgBox s
ts.Close
End Sub

And here is an example of using a path name witth a scripting object

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
 

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