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:
-
Start Visual Basic, og opprett deretter en ny Standard EXE. Som standard opprettes et skjema med navnet Skjema1.
-
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 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 -
Legg til følgende kode i delen Generelle deklarasjoner i skjemamodulen.
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 -
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