ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Why is my variable assignment not working? (https://www.excelbanter.com/excel-programming/397277-why-my-variable-assignment-not-working.html)

Ayo

Why is my variable assignment not working?
 

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!!!

joel

Why is my variable assignment not working?
 
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



"Ayo" wrote:


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!!!


Bill Renaud

Why is my variable assignment not working?
 
Works fine on my machine (Win ME, Excel 2000).

--
Regards,
Bill Renaud




Michael

Why is my variable assignment not working?
 
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

--
If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.




"Ayo" wrote:


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!!!


Bill Renaud

Why is my variable assignment not working?
 
Joel is right. (I tested with a full name when I called the routine.)

--
Regards,
Bill Renaud




Ayo

Why is my variable assignment not working?
 
Thanks Joel I will try this.

"Joel" wrote:

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



"Ayo" wrote:


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!!!



All times are GMT +1. The time now is 05:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com