|
-
Jul 26th, 2005, 10:56 AM
#1
Thread Starter
Junior Member
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
-
Jul 26th, 2005, 11:05 AM
#2
Re: Saving a Text File
To save :
VB Code:
Open Filename For Output As #1
Print #1, TextBox.Text
Close #1
Has someone helped you? Then you can Rate their helpful post. 
-
Jul 26th, 2005, 12:06 PM
#3
Thread Starter
Junior Member
Re: Saving a Text File
 Originally Posted by manavo11
To save :
VB Code:
Open Filename For Output As #1
Print #1, TextBox.Text
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.
-
Jul 26th, 2005, 12:13 PM
#4
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:
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.
-
Jul 26th, 2005, 12:19 PM
#5
Thread Starter
Junior Member
Re: Saving a Text File
Okay, now the error reads, "Runtime error. Object required." What is it I am doing wrong? Thanks, Dennis
-
Jul 26th, 2005, 12:21 PM
#6
Re: Saving a Text File
Show me what you did, and bold that line that causes the error.
-
Jul 26th, 2005, 12:23 PM
#7
Fanatic Member
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.
-
Jul 26th, 2005, 12:35 PM
#8
Thread Starter
Junior Member
Re: Saving a Text File
 Originally Posted by Hack
Show me what you did, and bold that line that causes the error.
VB Code:
Private Sub cmd_save_Click()
[B]Open "mytextfile.txt" For Append As #1 [/B]
Print #1, TextBox.Text
Close #1
End Sub
-
Jul 26th, 2005, 12:37 PM
#9
Thread Starter
Junior Member
Re: Saving a Text File
 Originally Posted by Hack
Show me what you did, and bold that line that causes the error.
VB Code:
Private Sub cmd_save_Click()
[B]Open "mytextfile.txt" For Append As #1 [/B]
Print #1, TextBox.Text
Close #1
End Sub
-
Jul 26th, 2005, 12:39 PM
#10
Thread Starter
Junior Member
Re: Saving a Text File
 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?
-
Jul 26th, 2005, 12:43 PM
#11
Fanatic Member
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:
with commondialog
.filename = vbnullstring
.showsave
end with
open commondialog.filename for append as #1
print #1, text1.text
close #1
-
Jul 26th, 2005, 12:47 PM
#12
Fanatic Member
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:
Private Sub Command1_Click()
cd1.ShowSave
Open cd1.FileName For Output As #1
Print #1, Text1.Text
Close #1
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.
-
Jul 26th, 2005, 12:52 PM
#13
Fanatic Member
Re: Saving a Text File
Actually use this code instead:
VB Code:
Private Sub Command1_Click()
cd1.ShowSave
On Error Resume Next
Open cd1.FileName For Output As #1
Print #1, Text1.Text
Close #1
End Sub
So if cancel is pushed, then there isn't an error.
-
Jul 26th, 2005, 01:01 PM
#14
Re: Saving a Text File
 Originally Posted by dmyhand
VB Code:
Private Sub cmd_save_Click()
[B]Open "mytextfile.txt" For Append As #1 [/B]
Print #1, TextBox.Text
Close #1
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.
-
Jul 26th, 2005, 01:19 PM
#15
Thread Starter
Junior Member
Re: Saving a Text File
 Originally Posted by JBD2
Actually use this code instead:
VB Code:
Private Sub Command1_Click()
cd1.ShowSave
On Error Resume Next
Open cd1.FileName For Output As #1
Print #1, Text1.Text
Close #1
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
-
Jul 26th, 2005, 01:22 PM
#16
Thread Starter
Junior Member
Re: Saving a Text File
 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
-
Jul 26th, 2005, 01:25 PM
#17
Fanatic Member
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:
Private Sub Command1_Click()
'Open the Common Dialog Box with A "Save" Button
cd1.ShowSave
'If Cancel Is pushed ignore the code after this line
On Error Resume Next
'Create a text file with the filename from common dialog (save)
Open cd1.FileName & ".txt" For Output As #1
Print #1, Text1.Text
Close #1
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.
-
Jul 26th, 2005, 01:37 PM
#18
Thread Starter
Junior Member
Re: Saving a Text File
 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:
Private Sub Command1_Click()
'Open the Common Dialog Box with A "Save" Button
cd1.ShowSave
'If Cancel Is pushed ignore the code after this line
On Error Resume Next
'Create a text file with the filename from common dialog (save)
Open cd1.FileName & ".txt" For Output As #1
Print #1, Text1.Text
Close #1
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!
-
Jul 26th, 2005, 01:42 PM
#19
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
-
Jul 26th, 2005, 01:44 PM
#20
Fanatic Member
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:
Private Sub Form_Load()
cd1.InitDir = "C:\"
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|