Sammendrag

Med automatisering kan du gjøre programmatisk nesten hva som helst som brukeren kan gjøre manuelt i Microsoft Office Word. Hvis du har mye tekst du vil skrive inn og formatere, kan det imidlertid kreve mye kode. Hvis du kan representere dataene som en RTF-streng (Rikt tekstformat), kan du ofte redusere automatiseringskoden. Du kan opprette en RTF-streng, kopiere RTF-strengen til utklippstavlen og deretter lime inn RTF-strengen i dokumentet.Denne artikkelen beskriver hvordan du bygger et enkelt Visual Basic eksempel som starter Word, oppretter et nytt dokument og legger til litt formatert tekst i dokumentet ved hjelp av en forhåndsbygd RTF-streng.

Mer informasjon

Følg disse trinnene for å opprette eksempelprosjektet:

  1. Start Visual Basic, og opprett deretter en ny Standard EXE. Som standard opprettes et skjema med navnet Skjema1.

  2. Legg til en Kommandoknapp i skjemaet, dobbeltklikk kommandoknappen, og legg deretter til følgende kode i Klikk-hendelsen.

    '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. Legg til følgende kode i delen Generelle deklarasjoner i skjemamodulen.

    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. Trykk F5-tasten for å kjøre prosjektet. Word starter, og deretter opprettes et nytt dokument som inneholder formatert tekst.

Referanser

Hvis du vil ha mer informasjon og eksempler for å Office løsninger, kan du gå til følgende Microsoft-nettsteder:

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

Trenger du mer hjelp?

Vil du ha flere alternativer?

Utforsk abonnementsfordeler, bla gjennom opplæringskurs, finn ut hvordan du sikrer enheten og mer.