Does anyone have any good resource that I can refer to on exporting text in a richtextbox to a word file?
6 replies to this topic
#1
Posted 27 April 2011 - 08:29 AM
|
|
|
#2
Posted 27 April 2011 - 08:42 AM
Try saving it as .doc format. I did a quick test on it just entering plain text, try it yourself, It's worth a shot :)
#3
Posted 27 April 2011 - 09:08 AM
This is a good question. Surprisingly the solution is very simple (Blimp got it).
You can just write the RichTextBox's RTF property to a file with the extension .doc
Told you it was simple!
Here is some code that could do this:
This will create a file called Test.doc on your desktop.
Let us know if this solved your problem :)
Edit: you could also save the file with the extension .rtf as Micro$oft Word understands this format as well.
You can just write the RichTextBox's RTF property to a file with the extension .doc
Told you it was simple!
Here is some code that could do this:
Dim Path As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\Test.doc" Dim sw As New System.IO.StreamWriter(Path) sw.Write(RichTextBox1.Rtf) sw.Flush() sw.Close()
This will create a file called Test.doc on your desktop.
Let us know if this solved your problem :)
Edit: you could also save the file with the extension .rtf as Micro$oft Word understands this format as well.
#4
Posted 28 April 2011 - 05:15 AM
Excellent. Thanks so much. I thought it would be much more difficult than that. So here is the code I'm using:
SaveFileDialog1.ShowDialog() Dim svLocation As String = SaveFileDialog1.FileName Dim sw As New System.IO.StreamWriter(svLocation, True) sw.Write(RichTextBox1.Rtf) sw.Flush() sw.Close()
#5
Posted 28 April 2011 - 09:44 AM
Awesome.
I would modify your code with:
The Try...Catch protects you from file access exceptions (very common) and the If...Then will only be executed if you hit Save on the dialog.
Enjoy!
I would modify your code with:
Dim sd As New SaveDialog() If sd.ShowDialog() = DialogResult.Ok Then Try Dim svLocation As String = sd.FileName Dim sw As New System.IO.StreamWriter(svLocation, True) sw.Write(RichTextBox1.Rtf) sw.Flush() sw.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End If
The Try...Catch protects you from file access exceptions (very common) and the If...Then will only be executed if you hit Save on the dialog.
Enjoy!
#6
Posted 28 April 2011 - 09:59 AM
Also, I would add a filter and a default extension to the SaveDialog
This will make sure there's a .doc on the end of the filename
Hope this helps
Dim sd As New SaveDialog() sd.DefaultExtension = "doc" sd.Filter = "Microsoft Word Document (.doc)|*.doc"
This will make sure there's a .doc on the end of the filename
Hope this helps
#7
Posted 28 April 2011 - 11:22 AM
Thank you.
I added the filter in the options of the savedialog: "Word Document|*.doc|RTF|*.rtf"
Which is better, to set the options in the "options" settings or to set them in the code? or does it really make a difference?
I added the filter in the options of the savedialog: "Word Document|*.doc|RTF|*.rtf"
Which is better, to set the options in the "options" settings or to set them in the code? or does it really make a difference?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top










