Summary

With Automation, you can do programmatically almost anything that the user can do manually in Microsoft Office Word. However, if you have lots of text that you want to enter and to format, it might require lots of code. If you can represent the data as a Rich Text Format (RTF) string, you can frequently reduce the Automation code. You can create an RTF string, copy the RTF string to the clipboard, and then paste the RTF string into the document. This article describes how to build a simple Visual Basic example that starts Word, creates a new document, and adds some formatted text to the document by using a pre-built RTF string.

More Information

To create the example project, follow these steps:

  1. Start Visual Basic, and then create a new Standard EXE. By default, a form that is named Form1 is created.

  2. Add a CommandButton to the form, double-click the CommandButton, and then add the following code to the Click event.

    'sRTF represents the rich-text-formatted string to paste into WordDim sRTF As StringsRTF = "{\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 clipboardDim lSuccess As LongDim lRTF As LongDim hGlobal As LongDim lpString As LonglSuccess = OpenClipboard(Me.hwnd)lRTF = RegisterClipboardFormat("Rich Text Format")lSuccess = EmptyClipboardhGlobal = GlobalAlloc(GMEM_MOVEABLE Or GMEM_DDESHARE, Len(sRTF))lpString = GlobalLock(hGlobal)CopyMemory lpString, ByVal sRTF, Len(sRTF)GlobalUnlock hGlobalSetClipboardData lRTF, hGlobalCloseClipboardGlobalFree hGlobal'Paste into a new Word documentDim oWord As ObjectDim oDoc As ObjectSet oWord = CreateObject("word.application")Set oDoc = oWord.Documents.AddoWord.Selection.PasteoWord.Visible = True
  3. Add the following code to the General Declarations section of the Form module.

    Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function RegisterClipboardFormat Lib "user32" Alias _    "RegisterClipboardFormatA" (ByVal lpString As String) As LongPrivate Declare Function EmptyClipboard Lib "user32" () As LongPrivate Declare Function CloseClipboard Lib "user32" () As LongPrivate Declare Function SetClipboardData Lib "user32" ( _    ByVal wFormat As Long, ByVal hMem As Long) As LongPrivate Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _    ByVal dwBytes As Long) As LongPrivate 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 LongPrivate Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As LongPrivate Declare Function GlobalFree Lib "kernel32" Alias "GlobalFree" ( _     ByVal hMem As Long) As LongPrivate Const GMEM_DDESHARE = &H2000Private Const GMEM_MOVEABLE = &H2
  4. Press the F5 key to run the project. Word starts, and then a new document is created that contains formatted text.

References

For more information and for samples for developing Office solutions, visit the following Microsoft Web sites:

http://support.microsoft.com/ofd http://msdn.microsoft.com/office

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.