Community discussion forum

Loading and saving text files

Tags: vb6 Australia
  • 2 years ago
    How would I make it so that when I press a command button, a load box pops up, and I can open a file (.txt, .ini, .vbs, .bat, .htm or .html) into a text box. I would also like to be able to "save as" in a similar fashion.

    How?


    Thanks,

    BP
  • 2 years ago
    have you heard of somethign called Common Dialogues ???

    well there is a native control that is provided by the VB 6 called Microsoft Common Dialogue Control 6.0 (SP6) insert it in your project by righ clicking the toolbox and selecting the components.....from there you can select that said control and press ok.....

    now drag that new control over your form.....it will shape like a box.....rename it to a convenient name say cd1

    now in the click event of the button type this...

    cd1.Filter = HTML Files(*.html)(*.htm)|*.html;*.htm | Batch Files(*.bat)|*.bat| all supported _ files |*.bat;*.html;*.htm"
    cd1.ShowOpen




    this can also be done for saving files

    jsut change the filter according to your needs and also instead of .ShowOpen use cd1.ShowSave

    post again if you have any problem Smiley Face [:)]













  • 2 years ago
    I normally rely on the defaul tools, and occasionally internet tools, so I didn't know about that, thanks.
    This is my code: Private Sub Command1_Click()
    cd1.Filter = HTML Files(*.html)(*.htm)|*.html;*.htm | Batch Files(*.bat)|*.bat| all supported _ files |*.bat;*.html;*.htm"
    cd1.ShowOpen
    End Sub


    What is wrong with it, I get syntax error when I use it :(


    And how to load the file into the text box?

    Thanks,

    BP
  • 2 years ago
    Option Explicit
    Dim textfile As Integer
    Dim Filter As Integer
    Private Sub cmdColour_Click()
    On Error GoTo errhandler
    CommonDialog1.CancelError = True

    ' Display the Colour dialog box
    CommonDialog1.ShowColor
    ' Set the forms background colour to the one selected
    Me.BackColor = CommonDialog1.Color
    Exit Sub
    errhandler:
    Select Case Err
    Case 32755 ' Dialog Cancelled

    Case Else
    MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
    End Sub
    Private Sub cmdOpen_Click()
    On Error GoTo errhandler
    CommonDialog1.CancelError = True
    ' Set flags
    CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNPathMustExist + cdlOFNFileMustExist
    ' Set filters
    CommonDialog1.Filter = "All Files (*.*)|*.*|RTF (*.rtf)|*.rtf|Text Files (*.txt)|*.txt"

    ' Display the Save dialog box
    CommonDialog1.FileName = ""
    CommonDialog1.ShowOpen
    txtData.Text = "File Selected: " & CommonDialog1.FileName
    Exit Sub
    errhandler:
    Select Case Err
    Case 32755 ' Dialog Cancelled

    Case Else
    MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
    End Sub


    Private Sub cmdSave_Click()
    On Error GoTo errhandler
    CommonDialog1.CancelError = True
    ' Set flags
    CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt + cdlOFNPathMustExist
    ' Set filters
    CommonDialog1.Filter = "All Files (*.*)|*.*|RTF (*.rtf)|*.rtf|Text Files (*.txt)|*.txt"
    ' Specify default filter

    ' Display the Save dialog box
    CommonDialog1.FileName = ""
    CommonDialog1.ShowSave
    ' Set the label values
    txtData.Text = "File Selected: " & CommonDialog1.FileName
    Exit Sub
    errhandler:
    Select Case Err
    Case 32755 ' Dialog Cancelled

    Case Else
    MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
    End Sub


    Is my current code, i got the sample off this site. But it doesn't actually save or open the file's data. When you open it, it just says the filepath.And saving doesn't appear to do anything at all. So how do I extract the data of the page and be able to save it?


    Thanks,


    BP
  • 2 years ago

    BP,

    The common dialog Load/Save will return you the filename (with path) the user typed in.  You will have to add the code that actually saves the file yourself, as the dialog obviously doesn't know anything about what data to save in which format.

    Regards,

    Erwin

  • 2 years ago
    Yeah, I read the code and saw that....but as all my curently developed applications are just simple games or cheap tricks, I don't know the code :( I tried Googling, but nothing important came up, Thanks, BP
  • 2 years ago

    Please use the VB Help on topics like Open, Print#, Input# or something like "Working with files".

    As an alternative, you can use a FileSystemObject with methods like CreateTextfile, Write, WriteLine, Read or ReadLine. This solution is more advanced and can easily be upgraded to VB.Net classes.

  • 2 years ago
    what kind of error message you are getting ?? could you post your part of code?? I am using the same code to create my own wordpad......is working afterall....

    please reply...Big Smile [:D]

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback