+ Reply to Thread
Results 1 to 2 of 2

Thread: Problem with writing to text file from VBA

  1. #1
    Registered User
    Join Date
    06-01-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    45

    Problem with writing to text file from VBA

    I'm new to this forum, so greetings!

    Anyways, the problem I have right now is a 'runtime 70 error' (permission error).

    Yes, I do have full access to the file and folder where the text file is located - which is why I'm asking, why does the following code prompt me with a permission error?

    *** Reading from the file works. Writing does not (Output does not work).

    This is the first sub, which basically checks to see weather the text file exists or not. If it does, it reads the info from it. If not, it creates a text file, then it prompts the user with a form to input the info.
    Public Sub userInfo()
    
    Dim fn As Integer
    Dim oFSO, sFileName
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    fn = FreeFile
    sFileName = "H:\gstLetterInfo.txt"
    
    If oFSO.FileExists(sFileName) Then
    
        GoTo readFile
        
    Else
    
        Set objfso = CreateObject("Scripting.FileSystemObject")
        Set objFile = objfso.CreateTextFile(sFileName)
        
        MsgBox "This is your first time using this macro. Please enter your contact information.", vbInformation, "First Time Use"
            
        frmInfo.Label_User.Caption = userID
        frmInfo.Show
        
    End If
    
    Exit Sub
    
    readFile:
    
        Open sFileName For Input As #fn '' Didn't even get to this point yet
        
            Line Input #fn, userName
            Line Input #fn, userPhone1
            Line Input #fn, userPhone2
            Line Input #fn, userPrinter
            MsgBox userName & userPhone1 & userPhone2 & userPrinter '''''
        Close #fn
    
    End Sub
    The next portion of code is located in the form. This is where I get the error when trying to write to the text file.
    Private Sub Button_Submit_Click()
    
    Dim fn As Integer
    
    sFileName = "H:\gstLetterInfo.txt"
    fn = FreeFile
    
    ''
    ''<CODE>''
    ''
    
    userName = Text_Name.Value
    userPhone1 = Text_Phone1.Value
    userPhone2 = Text_Phone2.Value
    userPrinter = Text_Printer.Value
    
    Open sFileName For Output As #fn
    
        Print #fn, userName
        Print #fn, userPhone1
        Print #fn, userPhone2
        Print #fn, userPrinter
        
    Close #fn
    
    Unload Me
    
    End Sub
    Kind of a long question, hopefully it has a short answer!

    Thanks for your help,

    - Shane
    Last edited by Shane O; 06-01-2010 at 10:45 AM.

  2. #2
    Registered User
    Join Date
    06-01-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Problem with writing to text file from VBA

    I solved my own problem...

    It worked by taking out:
    Set objfso = CreateObject("Scripting.FileSystemObject")
    Set objFile = objfso.CreateTextFile(sFileName)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0