Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

RTF into word through VBA (or how to use pastespecial)

RTF into word through VBA (or how to use pastespecial)

RTF into word through VBA (or how to use pastespecial)

Some background:  we have a VB6 app that allows the user to enter data into several different Rich Text Boxes.  The RTB data is then written into a SQl Server database.  If you look at the text/string in the database it contains the Rich Text Formatting characters.

We want to use a Word (sadly 97) Macro to query the RTF from the database and place it appropriately in report template.

One possible solution would be to write each response to a RTF file, and then import each RTF file into the Word document.  However, I'm convinced there has to be a better way to do this.

I know the SELECTION has the PASTESPECIAL method which allows you to paste using RTF, but I don't know how to get the string read from the Db onto the clipboard for PASTESPECIAL to be used.  How is the clipboard accessed through VBA?

RE: RTF into word through VBA (or how to use pastespecial)

The clipboard object can be accessed directly:


If you want to get the best response to a question, please check out FAQ222-2244 first

For tsunami relief donations

'If we're supposed to work in Hex, why have we only got A fingers?'

www.essexsteam.co.uk for steam enthusiasts

RE: RTF into word through VBA (or how to use pastespecial)

I think you want to use something like...


Clipboard.SetText RichTextBox1.TextRTF, vbCFRTF

Then just use the Paste command in word...
such as:





Dim wdApp as object
Set wdApp = getobject(,"Word.Application")

Have Fun, Be Young... Code BASIC


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.

RE: RTF into word through VBA (or how to use pastespecial)

I tried accessing the Clipboard directly, as suggested.  But, here is the problem.  Unlike VB, when you're writing a macro in VBA, the clipboard object is not available.  This produces an object required error.

From within VB, I used the definition to determine that the clipboard object is a member of GLOBALS found in the VB6.OLB library.   I then tried to include the VB6.OLB under tools references, so the clipboard object would be available within the macro.  But this causes the macro to die an ugly death where I actually have to reboot the system before I am able to access WORD again.

Any further thoughts...

RE: RTF into word through VBA (or how to use pastespecial)

vladk, thank you for the link.

Will that work for the text with Rich Text Formatting?

RE: RTF into word through VBA (or how to use pastespecial)

Alright, I got it!!!

Download the following module to access the clipboard

oopicide.mcglothin.info/ Oopic_5.1.22/Source/modClipboard.bas

As the module comes it facilitates the cut & paste of plain text.  So we have to make a couple of modifications to maintain our RTF.

First of all, CF_RTF is not in the set of standardized formats, Microsoft refers to it as a synthesized format.  what this means to us, is that we will need to register the format so it will be recognized.  Add the following to the header section of modClipboard.bas


Declare Function RegisterClipboardFormat _
    Lib "user32" _
    Alias "RegisterClipboardFormatA" _
(ByVal lpString As String) As Long

In SetClipboardData we will add a line, and change a line, but first dim CF_RTF as Long.


     CF_RTF = RegisterClipboardFormat("Rich Text Format")
     ' Copy the data to the Clipboard.
     lngClipMem = WinSetClipboardData(CF_RTF, lngHoldMem)
The change occurs in the 2nd line where CF_TEXT is replaced with our newly registered CF_RTF.

That accomplishes our goal.  We may now query the database to retrieve the RTF fields as strings, paste them to the clipboard using SetClipboardData, and then paste them into the word report using the selection.pastespecial.

The following is a crude example to illustrate the process (sans the db references):


Sub testpastertf()
Dim strSelection As String

Dim rtb As New RichTextBox

strSelection = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\froman\fprq2\fcharset0 Garamond;}}" & _
"{\colortbl ;\red0\green128\blue0;\red255\green0\blue0;\red0\green0\blue128;}" & _
"{\*\generator Msftedit;}\viewkind4\uc1\pard\f0\fs20\par" & _
"\par" & _
" This is \cf1 Some \cf2 fancy \cf3\b\i\f1\fs36 RTF \cf0\b0\i0\f0\fs20 text\par}"

rtb.TextRTF = strSelection

SetClipboardData rtb.TextRTF


End Sub

Sub pasteRTF()

    Selection.PasteSpecial Link:=False, DataType:=wdPasteRTF, Placement:= _
        wdInLine, DisplayAsIcon:=False
End Sub

To test the code, create a word template/macro paste in the above code. Add the modClipboard.bas module with the changes we discussed.  Make sure under Tools - References you have added "Microsoft Rich Textbox Control"  (you may need to browse to the OCX).  And your good to go...

Happy coding.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close