Greetings folks,

I need to load a word document file into a RichTextBox component, It can't be done directly, so, I need first, convert it to a RichText Format File and then load it to the component. Any Ideas?

Thanks

Omar

Dani AI

Generated

's idea of using Word to produce an RTF and then loading that RTF into the RichText control is a practical desktop solution. The reason the named constant (wdFormatRTF) came up as "not recognized" is that the code was using late binding (no Word type library referenced). Named Word constants are only available when you add a reference to the Word object library (early binding); otherwise you must use the numeric value or supply the enum yourself. See Microsoft guidance on referencing COM/Office type libraries for details. (learn.microsoft.com)

If the goal is simply "display the RTF in a WinForms RichTextBox", write the file to disk (or produce the RTF in memory) and then load it with LoadFile or set the control's Rtf property. Example (VB.NET):

RichTextBox1.LoadFile("C:\\x.rtf", RichTextBoxStreamType.RichText)

LoadFile also accepts a Stream so you can avoid temporary files. The WinForms RichTextBox docs explain the supported modes and overloads. (learn.microsoft.com)

If automation is acceptable (interactive, desktop tooling), use Word's SaveAs/SaveAs2 to produce RTF and the WdSaveFormat enum to pick formats. The Word VBA documentation covers SaveAs2 and the WdSaveFormat values (RTF is one of the enum members). If this code will run unattended on a server, do not automate the Word desktop — Microsoft explicitly warns that Office Automation is unsupported and unreliable in server-side or noninteractive scenarios; for server or batch conversion prefer Open XML approaches, LibreOffice headless conversion, or a dedicated conversion library. (learn.microsoft.com)

Troubleshooting checklist: confirm Word is installed and accessible to the account running the code; add the Microsoft Word Object Library reference if you want named constants; check paths/permissions; release COM objects and call Quit when automating; test the produced RTF in WordPad to verify the RTF itself. Also remember the RichTextBox supports a subset of RTF features, so very Word-specific formatting (headers/footers, advanced fields) may not appear exactly as in Word. (learn.microsoft.com)

Recommended Answers

All 6 Replies

Hi,

Try this, I haven't been able to test it:

Dim Oword As Object
Dim Odoc As Object

Private Sub Command1_Click()
  Set Oword = CreateObject("Word.Application")
  Set Odoc = Oword.documents.open("C:\temp.doc")
  Odoc.SaveAs FileName:="c:\x.rtf", FileFormat:=wdFormatRTF
  Oword.quit

  Me.RichTextBox1.FileName = "C:\x.rtf"
End Sub

Thanks KB ,

But, now I can't make it work, the "wdFormatRTF" is a non recognized variable :S, I tried to look for other options, but I can't find any related (and relevant) info about those options on the "Word.Application" object, maybe you can show me how can I accomplish this. Thanks

Hi,
Just replace the wdFormatRTF with number 6 (this code is for VB6, if you are using different IDE please advise).

Dim Oword As Object
Dim Odoc As Object

Private Sub Command1_Click()
  Set Oword = CreateObject("Word.Application")
  Set Odoc = Oword.documents.open("C:\temp.doc")
  Odoc.SaveAs FileName:="c:\x.rtf", FileFormat:=6
  Oword.quit
Me.RichTextBox1 = ""
  Me.RichTextBox1.FileName = "C:\x.rtf"
End Sub

Thanks man!!,

That worked great!!, now I'm trying with other formats, I'll post how i'm doing with it...

ok i found this thread through google and im quite a novice at scripting but where exactly would i incert that bit of code you posted in my MS word documents code?

First, it's strongly frowned upon to post to threads that are more than three months old. Even if you have a question regarding an old thread, then you would start a new thread, and post a link to the old thread with your question. This will help to ensure that you get a warm and helpful welcome instead of the inevitable flaming that is sure to follow posting to old threads.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.