Loading a richtextbox the text in an MS word Doc

QADUDE

Member
Joined
Dec 30, 2009
Messages
12
Programming Experience
3-5
How do I load the text in word document with a known file path into a RTB
 
You can load the document if it is saved as RTF format, with LoadFile method.
 
You can load the document if it is saved as RTF format, with LoadFile method.

Thanks for your reply johnH

See my code below

The rich text box text is sent to a folder and generates a word doc using this code. And I have cheched it exsits.

path =Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\CardIndex" (I have given this info just show how the file path is constructed the code in question starts at DocPath =


DocPath = String.Concat(path & "\A\Atext.rtf")


Dim sWriter As New IO.StreamWriter(DocPath)


sWriter.Write(RichTextBox1.Text)


sWriter.Close()

Loading the file text back to the rich text box as follows

DocPath = String.Concat(path & "\A\Atext.rtf")

If File.Exists(DocPath) Then
RichTextBox1.LoadFile(DocPath)

End If
Using the loadfile method generates a file format error.
 
You are writing a plain text file, and that LoadFile version expects a rtf file. You have to make up your mind which format you want to use. For example to save a rtf file you can use the SaveFile method. Both SaveFile/LoadFile methods also allows you to specify what format to use with the fileType parameter.
 
You are writing a plain text file, and that LoadFile version expects a rtf file. You have to make up your mind which format you want to use. For example to save a rtf file you can use the SaveFile method. Both SaveFile/LoadFile methods also allows you to specify what format to use with the fileType parameter.

Hi John thanks for your response
I have looked at the file created by the first part of the code and it is recoeded as in Rich Text Format
 
No, it is plain text. You have written the .Text content and that is plain text. Giving a file some extension doesn't mean its contents suddenly becomes that format.
In addition you have implicitly saved using Utf-8 encoding, something LoadFile has no option for reading.
Why complicate things? Just use SaveFile method to save a relevant format and LoadFile method to load same format.
 
Back
Top