Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Updating Text File from List Box (Word 2003)

    I have a form which reads data from a text file into a list box. I can then add or remove items from the list box, but I can't seem to write the information back to the file.
    The procedure I am using is:

    1 - on initialisation the form reads a text file and loads the records into a list box
    2 - using command buttons on the form the user can add or remove items from the list box
    3 - on Save, I want the contents of the list box to be written back to the text file, overwriting anything in the file.

    I can do 1 and 2, but can't get 3 to work.

    I've attached a document with the code for the form. The form uses a text file called DocumentTypes.txt and this looks like this:

    Fax,F
    Letter,L
    Project Estimate,PE
    Site Review,SR

    Unfortunately I can't seem to attach two files.

    Any help would be appreciated. I've been trying to use list, listcount etc, along with open for input, open for output, print and write - all to no avail.
    Attached Files Attached Files

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Updating Text File from List Box (Word 2003)

    Try this:

    Private Sub cmdSave_Click()
    Dim iCnt As Integer
    On Error GoTo Error

    Open strDocumentTypesFile For Output As #1

    For iCnt = 0 To lstDocumentTypes.ListCount - 1
    textline = lstDocumentTypes.List(iCnt)
    Print #1, textline
    Next iCnt

    Close #1

    Unload UserForm2
    Exit Sub

    Error:
    MsgBox "Error writing to " & strDocumentTypesFile & vbCrLf & _
    "At record " & iCnt & vbCrLf & vbCrLf & _
    Err.Description, , "Error " & Err.Number
    End Sub

    Note: if you want to attach multiple files, you can zip them and attach the zip file.

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Updating Text File from List Box (Word 2003)

    Thanks for your prompt reply Hans. I've just had a frustrating day playing around with this, and your code works beautifully.

Posting Permissions

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