I know how to save information in a richtextbox to file, but the code doesn't work for a text box. Any suggestions?
Printable View
I know how to save information in a richtextbox to file, but the code doesn't work for a text box. Any suggestions?
To save data from a text box (text1)
open "Myfile.txt" for output as 1
print #1,text1.text
close #1
If the text box is not multiline, this gives 1 line in the file. If it is multiline, then it gives lots of lines
Cheers
Chris
Great, thanks!!!
I tried this code, but it's not 100% working with the rich text box. Sometimes all the Cr and Lf are gone and the file has only one line, and other times the cr and lf are there. strange~~~
From Help File(RichTextBox Control) :
You can easily open or save an RTF file with the RichTextBox control by using the LoadFile and SaveFile methods. To open a file, use a CommonDialog control to supply the path name as shown:
Private Sub OpenFile()
' The RichTextBox control is named "rtfData."
' The CommonDialog is named "dlgOpenFile."
' Declare a String variable for file name.
' Show the Open File dialog box, and set the
' variable to the filename.
Dim strOpen As String
dlgOpenFile.ShowOpen
strOpen = dlgOpenFile.FileName
' Use LoadFile method to open the file.
rtfData.LoadFile strOpen
End Sub
To save a file is just as easy, using the SaveFile method:
Private Sub SaveFile()
Dim strNewFile As String
dlgOpenFile.ShowSave
strNewFile = dlgOpenFile.FileName
rtfData.SaveFile strNewFile
End Sub
Thanks for your reply. The most crucial part is the .savefile method. the above code is also using this method and the problem is that the resulting txt file will lose all the formatting.... :(
To keep the formatting, pass rtfRTF as the flag.
If you want to save in plain text format (ASCII), pass rtfText as the flag.Code:RichTextBox1.SaveFile "C:\MyFile.rtf", rtfRTF
Code:RichTextBox1.SaveFile "C:\MyFile.rtf", rtfText