Library code snippets
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
.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.
Related articles
Related discussion
-
VB6, SQL 2005 & DMO
by elajaunie3 (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
video not working with visual basic
by Jupiter 2 (9 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
In your post, you write "Fortunately, Visual Basic offers several fairly simple ways to do so." What are the other ways?
The reason I don't like the copy/paste solution is that it copies over the original contents of the clipboard, and I cannot figure out how to restore the original clipboard AFTER I copy/paste the text into Word.
RTF to Word is no problem, but what if you've got formatted data in Word that you'd like to display on a VB form? A formatted table in Word won't translate to RTF, so you have to put a Word Doc control on your form. But I haven't figured out how to transfer that Word-formatted text from Word to VB, except via the clipboard. The following shows what works just within Word:
With Selection
.GoTo What:=wdGoToBookmark, Name:="B1"
Selection.Copy
.GoTo What:=wdGoToBookmark, Name:="B2"
Selection.Paste
End With
I'd like to pass a Word object, such as Selection, but I don't see how it can be passed to an ActiveX exe. If I store Selection.FormattedText in a string, it loses the formatting. Any suggestions?
This thread is for discussions of Transfer formatted text into Word.