How do I save to file?
-
I have a project that has a RichTextBox that a user can enter data. The text box is called txtOut. I have a CommonDialog control and a command control called cmdSave with this code attached: Private Sub cmdSave_Click() Dim sFileName As String sFileName = txtOut.Text '& theExtension ' Set CancelError is True CommonDialog1.CancelError = True On Error GoTo ErrHandler ' Set flags CommonDialog1.Flags = cdlOFNHideReadOnly ' Set filters CommonDialog1.Filter = "All Files (*.*)|*.*|Hex Files" & _ "(*.hex)|*.hex|Assembly Files (*.asm)|*.asm" ' Specify default filter CommonDialog1.FilterIndex = 2 ' Display the Open dialog box CommonDialog1.ShowSave ' Display name of selected file MsgBox CommonDialog1.FileName Exit Sub ErrHandler: 'User pressed the Cancel button Exit Sub End Sub Everything works fine except after I name the file to be saved and hit the save button in the dialog window, it doesn't create the save file. I'm guessing it doesn't see the data in the text box. Can someone give me some help please? Thanks!:)
-
I have a project that has a RichTextBox that a user can enter data. The text box is called txtOut. I have a CommonDialog control and a command control called cmdSave with this code attached: Private Sub cmdSave_Click() Dim sFileName As String sFileName = txtOut.Text '& theExtension ' Set CancelError is True CommonDialog1.CancelError = True On Error GoTo ErrHandler ' Set flags CommonDialog1.Flags = cdlOFNHideReadOnly ' Set filters CommonDialog1.Filter = "All Files (*.*)|*.*|Hex Files" & _ "(*.hex)|*.hex|Assembly Files (*.asm)|*.asm" ' Specify default filter CommonDialog1.FilterIndex = 2 ' Display the Open dialog box CommonDialog1.ShowSave ' Display name of selected file MsgBox CommonDialog1.FileName Exit Sub ErrHandler: 'User pressed the Cancel button Exit Sub End Sub Everything works fine except after I name the file to be saved and hit the save button in the dialog window, it doesn't create the save file. I'm guessing it doesn't see the data in the text box. Can someone give me some help please? Thanks!:)
It's because the CommonDialogs only display dialogs to gather information from the user. So the Open, Save and Print dialogs don't actually do the opening, saving or printing for you. You have to implement that yourself. So in your case you have to check that the user has succesfully OKed a filename and then use something like a StreamWriter to save your file.
-
It's because the CommonDialogs only display dialogs to gather information from the user. So the Open, Save and Print dialogs don't actually do the opening, saving or printing for you. You have to implement that yourself. So in your case you have to check that the user has succesfully OKed a filename and then use something like a StreamWriter to save your file.
-
I am not an experienced coder, is there sample code or information available on how to do that? Thanks!:)
Here's something I've done in the past, you should be able to pull it apart for your own stuff:
Private Sub SaveINIFile() Dim strCurrentSection As String Dim ptrRecord As INILayout If strFileName = "" Then Return ptrFileWriter = New System.IO.StreamWriter(strFileName) For Each strCurrentSection In colINISections ptrFileWriter.WriteLine("[" & strCurrentSection & "]") For Each ptrRecord In colINIEntries If ptrRecord.strSection = strCurrentSection Then ptrFileWriter.WriteLine(ptrRecord.strHeader & "=" & ptrRecord.strValue) End If Next Next ptrFileWriter.Close() End Sub
-
I am not an experienced coder, is there sample code or information available on how to do that? Thanks!:)
-
It's quite simple
Open CommonDialog1.FileName For Output As #1
Print #1, "Hello!"
Print #1, "Green is Purple"
Print #1, "--------"
Print #1, Text1.TextClose #1
HTH ===== Phlip Always proofread carefully to see if you any words out
Thanks to everyone for their help! This is what I ended up using: Private Sub cmdSave_Click() 'When users clicks Save 'note: the entire file is stored in a string CommonDialog1.Filter = "Hex files (*.HEX)|*.HEX" CommonDialog1.ShowSave 'display Save dialog If CommonDialog1.FileName <> "" Then Open CommonDialog1.FileName For Output As #1 Print #1, txtOut.Text 'save string to file Close #1 'close file End If End Sub