Results 1 to 20 of 20

Thread: Saving a Text File

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Saving a Text File

    I am using VB6, and I am trying to build a text editor for my wife, who needs to write using French and Spanish. Is there a tutorial which will show how to set up saving or printing the text in my text area? I need to make this editor so that she can save or print, depending on her choice. I have control buttons on the form. Thanks, Dennis

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Saving a Text File

    To save :

    VB Code:
    1. Open Filename For Output As #1
    2.  Print #1, TextBox.Text
    3. Close #1


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Re: Saving a Text File

    Quote Originally Posted by manavo11
    To save :

    VB Code:
    1. Open Filename For Output As #1
    2.  Print #1, TextBox.Text
    3. Close #1
    Okay, I tried this and it states that there is a path/file access error. Does this need a file name in the code before it can work? If it does, that isn't going to work for me. I am not going to be using the program and I don't know what my wife will name any file she creates with it.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Saving a Text File

    You will, in one way or another, have to pass a path to it, UNLESS you want the file to be created in the App.Path of your project. If you (or she) does care where it goes, then you can just do
    VB Code:
    1. Open "mytextfile.txt" For Append As #1
    and it will be created in your project folder.

    Otherwise, you would need to supply her with a textbox within which to type a specific path which you could then tack onto your open statement.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Re: Saving a Text File

    Okay, now the error reads, "Runtime error. Object required." What is it I am doing wrong? Thanks, Dennis

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Saving a Text File

    Show me what you did, and bold that line that causes the error.

  7. #7
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: Saving a Text File

    Why not use a common dialog box? It has a save/open interface. And then you can choose where to save it.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Re: Saving a Text File

    Quote Originally Posted by Hack
    Show me what you did, and bold that line that causes the error.
    VB Code:
    1. Private Sub cmd_save_Click()
    2. [B]Open "mytextfile.txt" For Append As #1  [/B]          
    3.  Print #1, TextBox.Text
    4. Close #1
    5. End Sub

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Re: Saving a Text File

    Quote Originally Posted by Hack
    Show me what you did, and bold that line that causes the error.
    VB Code:
    1. Private Sub cmd_save_Click()
    2. [B]Open "mytextfile.txt" For Append As #1  [/B]          
    3.  
    4. Print #1, TextBox.Text
    5. Close #1
    6. End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Re: Saving a Text File

    Quote Originally Posted by JBD2
    Why not use a common dialog box? It has a save/open interface. And then you can choose where to save it.
    How would this be done?

  11. #11
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Saving a Text File

    Easily, press ctrl+T and find "Microsoft Common Dialog Control" check it, and it will be on your toolbar. Then all you have to do it add it to your form and use code similar to this.

    VB Code:
    1. with commondialog
    2. .filename = vbnullstring
    3. .showsave
    4. end with
    5.  
    6. open commondialog.filename for append as #1
    7. print #1, text1.text
    8. close #1

  12. #12
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: Saving a Text File

    Ok first go:

    Project->Components

    and check off "Microsoft Common Dialog Control". Then click OK. On the menu to the left where the common dialog control was added, click it, and put it on the form as you would a timer. Rename it "cd1". In your "save" button, put this code:

    VB Code:
    1. Private Sub Command1_Click()
    2. cd1.ShowSave
    3. Open cd1.FileName For Output As #1
    4.  Print #1, Text1.Text
    5. Close #1
    6. End Sub

    When she saves it though she'll have to type "whateveryouwant.txt". You'll always have to have the .txt, but I'll see if I can get it so you dont have to.

  13. #13
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: Saving a Text File

    Actually use this code instead:

    VB Code:
    1. Private Sub Command1_Click()
    2. cd1.ShowSave
    3. On Error Resume Next
    4. Open cd1.FileName For Output As #1
    5.  Print #1, Text1.Text
    6. Close #1
    7. End Sub

    So if cancel is pushed, then there isn't an error.

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Saving a Text File

    Quote Originally Posted by dmyhand
    VB Code:
    1. Private Sub cmd_save_Click()
    2. [B]Open "mytextfile.txt" For Append As #1  [/B]          
    3.  
    4. Print #1, TextBox.Text
    5. Close #1
    6. End Sub
    The object that you don't have is Textbox.Text. The code is expecting a textbox by that name. Either hard code a name, or change that to the name of a valid textbox on your screen.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Re: Saving a Text File

    Quote Originally Posted by JBD2
    Actually use this code instead:

    VB Code:
    1. Private Sub Command1_Click()
    2. cd1.ShowSave
    3. On Error Resume Next
    4. Open cd1.FileName For Output As #1
    5.  Print #1, Text1.Text
    6. Close #1
    7. End Sub

    So if cancel is pushed, then there isn't an error.
    THANK YOU! This worked great. Is there a way I can set a default file format for the app to save to, since my wife is not technically-minded enough to remember? Also, for the "Open," and "Print" commands, Do I need an separate Common Dialog for each one? Thanks, again, Dennis

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Re: Saving a Text File

    Quote Originally Posted by Hack
    The object that you don't have is Textbox.Text. The code is expecting a textbox by that name. Either hard code a name, or change that to the name of a valid textbox on your screen.
    Yeah, I figured this out about 3 minutes after I said it still gave an error. When I fixed it, it quit giving the error. It just didn't do anything. No save, no nothing. The code for the common dialog from JBD2 worked great and I will use that instead. Thank you for your kind assistance. Dennis

  17. #17
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: Saving a Text File

    Open and Print are not "open" and "print" (confusing?), those commands just create the text file. I have edited the code so it tells you what each thing does and makes it so you just have to type the file name.

    VB Code:
    1. Private Sub Command1_Click()
    2. 'Open the Common Dialog Box with A "Save" Button
    3. cd1.ShowSave
    4. 'If Cancel Is pushed ignore the code after this line
    5. On Error Resume Next
    6. 'Create a text file with the filename from common dialog (save)
    7. Open cd1.FileName & ".txt" For Output As #1
    8.  Print #1, Text1.Text
    9. Close #1
    10. End Sub

    That code saves the .txt, if you need help on printing I dont know how to do that, sorry. Oh, and for the save thing, you only need to have one common dialog box.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Re: Saving a Text File

    Quote Originally Posted by JBD2
    Open and Print are not "open" and "print" (confusing?), those commands just create the text file. I have edited the code so it tells you what each thing does and makes it so you just have to type the file name.

    VB Code:
    1. Private Sub Command1_Click()
    2. 'Open the Common Dialog Box with A "Save" Button
    3. cd1.ShowSave
    4. 'If Cancel Is pushed ignore the code after this line
    5. On Error Resume Next
    6. 'Create a text file with the filename from common dialog (save)
    7. Open cd1.FileName & ".txt" For Output As #1
    8.  Print #1, Text1.Text
    9. Close #1
    10. End Sub

    That code saves the .txt, if you need help on printing I dont know how to do that, sorry. Oh, and for the save thing, you only need to have one common dialog box.

    KEWL!

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving a Text File

    This is new, and allows you to see the print dialog which is better than the cdc show printer. It has issues with setting the printer location. This requires another control, but works like a regular program's print routine.

    http://vbforums.com/attachment.php?attachmentid=38114

  20. #20
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: Saving a Text File

    Oh, and if she also won't know where she saved stuff you can set the default location of where the common dialog box looks to start, so when it opens you could automatically make it look in C:\ By doing this:

    VB Code:
    1. Private Sub Form_Load()
    2. cd1.InitDir = "C:\"
    3. End Sub

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