Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default Why is my variable assignment not working?

Works fine on my machine (Win ME, Excel 2000).

--
Regards,
Bill Renaud



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 791
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default 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   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Row Variable assignment JMay Excel Discussion (Misc queries) 3 June 28th 07 05:26 PM
Offset not working with variable B[_5_] Excel Programming 3 June 12th 07 06:45 PM
Variable not working Phil Excel Discussion (Misc queries) 2 April 11th 07 11:49 AM
Variable not working as email address Amber_D_Laws[_22_] Excel Programming 12 January 17th 06 05:56 PM
range value with variable not working? James[_24_] Excel Programming 5 October 4th 04 02:43 PM


All times are GMT +1. The time now is 01:47 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"