Transfer formatted text into Word

As you know, VB's Rich Textbox control is no ordinary textbox. With it,
users not only can enter multiline text; but also apply RTF formatting,
such as bold, italics and color. Often, you may want to transfer this
formatted text into Microsoft Word. Fortunately, Visual Basic offers
several fairly simple ways to do so.

One way consists of copying the formatted text into the Clipboard
object, using Automation to open the Word Application, and then
pasting the formatted code into a blank document. The following
procedure shows how to accomplish this (it assumes you've set a
reference to the Microsoft Word 8.0/9.0 Object library).

Dim wrdApp As Word.Application

Private Sub Form_Load()
    Set wrdApp = New Word.Application
End Sub

Private Sub Command2_Click()
    Clipboard.SetText RichTextBox1.TextRTF, vbCFRTF
    With wrdApp
        .ActiveDocument.SaveAs App.Path & "RTFDOC2.doc", _
        .Visible = True
    End With
End Sub

As you can see, this code uses Visual Basic's Clipboard object to
accomplish the transfer. First, it uses the SetText() method to
capture the text. Then, it adds a new blank document to the Word
application's Documents collection. At this point, the cursor, or
Selection, is positioned at the top of the document, so all the code
need do is execute the Paste command to paste the Clipboard contents
into the document. Our code then saves the new document and displays it.

You might also like...


ElementK Journals


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer Science is no more about computers than astronomy is about telescopes.” - E. W. Dijkstra