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
.Documents.Add
.Selection.Paste
.ActiveDocument.SaveAs App.Path
& "RTFDOC2.doc", _
wdFormatDocument
.Visible = True
.Activate
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.
Comments