Jump to content

Export Richtextbox to Word

- - - - -

  • Please log in to reply
6 replies to this topic

#1
HighKing Scott

HighKing Scott

    Newbie

  • Members
  • PipPip
  • 15 posts
Does anyone have any good resource that I can refer to on exporting text in a richtextbox to a word file?

#2
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
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
wwarren

wwarren

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
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:

        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
HighKing Scott

HighKing Scott

    Newbie

  • Members
  • PipPip
  • 15 posts
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
wwarren

wwarren

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
Awesome.

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
wwarren

wwarren

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
Also, I would add a filter and a default extension to the SaveDialog

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
HighKing Scott

HighKing Scott

    Newbie

  • Members
  • PipPip
  • 15 posts
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?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users