Ringkasan
Dengan otomatisasi, Anda dapat melakukan secara programatik hampir apa pun yang pengguna dapat melakukan secara manual di Microsoft Office Word. Namun, jika Anda memiliki banyak teks yang Anda inginkan untuk memasukkan dan memformat, mungkin memerlukan banyak kode. Jika Anda dapat menunjukkan data sebagai string Rich Text Format (RTF), Anda dapat sering mengurangi kode otomatisasi. Anda dapat membuat string RTF, Salin RTF string ke clipboard, dan kemudian tempel RTF string ke dokumen.
Artikel ini menjelaskan cara membuat contoh Visual Basic sederhana yang memulai Word, membuat dokumen baru, dan menambahkan beberapa teks yang diformat untuk dokumen menggunakan untai RTF pra-dibangun.
Informasi lebih lanjut
Untuk membuat proyek contoh, ikuti langkah-langkah berikut:
-
Mulai Visual Basic, dan kemudian membuat Standard EXE baru. Secara default, bentuk yang bernama Form1 dibuat.
-
Tambah CommandButton pada formulir, klik dua kali CommandButton, dan kemudian tambahkan kode berikut untuk Klik peristiwa.
'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 -
Menambahkan kode berikut ke bagian Deklarasi umum Formulir modul.
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 -
Tekan tombol F5 untuk menjalankan projek. Word dimulai, dan kemudian dokumen baru akan dibuat yang berisi teks yang diformat.
Referensi
Untuk informasi selengkapnya dan untuk sampel untuk mengembangkan solusi Office, kunjungi situs Web Microsoft berikut: