-
Dec 27th, 2007, 03:57 AM
#1
Thread Starter
Junior Member
Save Text File Question?
Why wont this code work
Code:
Set fso = New FileSystemObject
If fso.FileExists(App.Path & "MySavedText.txt") = False Then
fso.CreateTextFile App.Path & "MySavedText.txt", True
If fso.FileExists(App.Path & "MySavedText.txt") Then
Else
On Error GoTo ErrHandler
End If
End If
Set fso = Nothing
Exit Sub
ErrHandler:
What its doing is writing the file on my desktop no mattter what folder my program is in with the name "TheFolderMyExeisINMySavedText.txt"
-
Dec 27th, 2007, 04:20 AM
#2
Re: Save Text File Question?
Try doing a Debug.Print App.Path I suspect you'll see that there is no terminating "\"
-
Dec 27th, 2007, 04:40 AM
#3
Thread Starter
Junior Member
Re: Save Text File Question?
I dont understand
-
Dec 27th, 2007, 04:49 AM
#4
Re: Save Text File Question?
App.Path does not return the trailing "\" of the pathname.
So if your file is in, say, C:\MyFolder\MySubFolder\, App.Path will return:
C:\MyFolder\MySubFolder
You need to add the "\", something like:
Code:
Set fso = New FileSystemObject
If fso.FileExists(App.Path & "\MySavedText.txt") = False Then
fso.CreateTextFile App.Path & "\MySavedText.txt", True
If fso.FileExists(App.Path & "\MySavedText.txt") Then
Else
On Error GoTo ErrHandler
End If
End If
Set fso = Nothing
Exit Sub
The exception is when you're using the root directory (eg c:\) App.Path does then return the trailing "\" (That might be Operating System dependent, I'm not too sure)
-
Dec 27th, 2007, 04:54 AM
#5
Thread Starter
Junior Member
Re: Save Text File Question?
Originally Posted by Doogle
App.Path does not return the trailing "\" of the pathname.
So if your file is in, say, C:\MyFolder\MySubFolder\, App.Path will return:
C:\MyFolder\MySubFolder
You need to add the "\", something like:
Code:
Set fso = New FileSystemObject
If fso.FileExists(App.Path & "\MySavedText.txt") = False Then
fso.CreateTextFile App.Path & "\MySavedText.txt", True
If fso.FileExists(App.Path & "\MySavedText.txt") Then
Else
On Error GoTo ErrHandler
End If
End If
Set fso = Nothing
Exit Sub
The exception is when you're using the root directory (eg c:\) App.Path does then return the trailing "\" (That might be Operating System dependent, I'm not too sure)
You are my hero haha jk but thank you so much!
-
Dec 27th, 2007, 05:04 AM
#6
Addicted Member
Re: Save Text File Question?
i think you want to create a "MySavedText.txt" on target directory and want to be sure exactly that it is created.
it must be as follows
Code:
Dim MyFile As String
Set fso = New FileSystemObject
MyFile = App.Path & "\MySavedText.txt"
If Not fso.FileExists(MyFile) Then
fso.CreateTextFile MyFile, True
If Not fso.FileExists(MyFile) Then GoTo ErrHandler
End If
Exit Sub
ErrHandler:
You can do while you think that you can do
If you think my answer solve your question, please rate it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|