How To Paste RichText Formatted String into Word with Visual Basic Automation (258513)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0

This article was previously published under Q258513

SUMMARY

With Automation, you can do programmatically almost anything the user can do manually in Microsoft Word. However, if you have a lot of text you want to enter and format, it might require a lot of code. If you can represent your data as a RichText Format (RTF) string, you can often decrease the amount of Automation code. You can create an RTF string, copy the string to the clipboard, and then paste it into your document.

This article walks you through building a simple Visual Basic example that starts Microsoft Word, creates a new document, and adds some formatted text to it using a pre-built RTF string.

MORE INFORMATION

Follow the steps below to create the example project:
  1. Start Visual Basic and create a new Standard EXE. Form1 is created by default.
  2. Add a CommandButton to your form, double-click it, and then add the following code to the Click event:
    'sRTF represents the rich text formatted string to paste into Word
    Dim sRTF As String
    sRTF = "{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl" & _
           "{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}" & _
           "{\f2\froman\fprq2 Times New Roman;}}" & _
           "{\colortbl\red0\green0\blue0;\red255\green0\blue0;}" & _
           "\deflang1033\horzdoc{\*\fchars }{\*\lchars }" & _
           "\pard\plain\f2\fs24 Line 1 of \plain\f2\fs24\cf1" & _
           "inserted\plain\f2\fs24  file.\par }"
              
    'Copy the contents of the Rich Text to the clipboard
    Dim lSuccess As Long
    Dim lRTF As Long
    Dim hGlobal As Long
    Dim lpString As Long
    lSuccess = OpenClipboard(Me.hwnd)
    lRTF = RegisterClipboardFormat("Rich Text Format")
    lSuccess = EmptyClipboard
    hGlobal = GlobalAlloc(GMEM_MOVEABLE Or GMEM_DDESHARE, Len(sRTF))
    lpString = GlobalLock(hGlobal)
    
    CopyMemory lpString, ByVal sRTF, Len(sRTF)
    GlobalUnlock hGlobal
    SetClipboardData lRTF, hGlobal
    CloseClipboard
    GlobalFree hGlobal
       
    'Paste into a new Word document
    Dim oWord As Object
    Dim oDoc As Object
    Set oWord = CreateObject("word.application")
    Set oDoc = oWord.Documents.Add
    oWord.Selection.Paste
    oWord.Visible = True
     
    					
  3. Add the following code to the General Declarations section of your Form module:
    Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function RegisterClipboardFormat Lib "user32" Alias _
        "RegisterClipboardFormatA" (ByVal lpString As String) As Long
    Private Declare Function EmptyClipboard Lib "user32" () As Long
    Private Declare Function CloseClipboard Lib "user32" () As Long
    Private Declare Function SetClipboardData Lib "user32" ( _
        ByVal wFormat As Long, ByVal hMem As Long) As Long
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
        ByVal dwBytes As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        ByVal Destination As Long, Source As Any, ByVal Length As Long)
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalFree Lib "kernel32" Alias "GlobalFree" ( _
         ByVal hMem As Long) As Long
    
    Private Const GMEM_DDESHARE = &H2000
    Private Const GMEM_MOVEABLE = &H2
    					
  4. Press the F5 key to run your project. Microsoft Word launches and a new document is created that contains formatted text.

REFERENCES

For additional information and samples for developing Office solutions, please visit:

http://support.microsoft.com/ofd

http://msdn.microsoft.com/office

Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kbAutomation kbhowto KB258513 kbAudDeveloper