Results 1 to 6 of 6

Thread: Save Text File Question?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Posts
    19

    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"

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Save Text File Question?

    Try doing a Debug.Print App.Path I suspect you'll see that there is no terminating "\"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Posts
    19

    Re: Save Text File Question?

    I dont understand

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Posts
    19

    Re: Save Text File Question?

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

  6. #6
    Addicted Member BlueRose's Avatar
    Join Date
    Jan 2002
    Location
    ISTANBUL
    Posts
    245

    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
  •  



Click Here to Expand Forum to Full Width