Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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!!! |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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!!! |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Why is my variable assignment not working?
Works fine on my machine (Win ME, Excel 2000).
-- Regards, Bill Renaud |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
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!!! |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Why is my variable assignment not working?
Joel is right. (I tested with a full name when I called the routine.)
-- Regards, Bill Renaud |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
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!!! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Row Variable assignment | Excel Discussion (Misc queries) | |||
Offset not working with variable | Excel Programming | |||
Variable not working | Excel Discussion (Misc queries) | |||
Variable not working as email address | Excel Programming | |||
range value with variable not working? | Excel Programming |