Ringkasan
Dengan Otomatisasi, Anda bisa melakukan hampir semua hal yang bisa dilakukan pengguna secara manual dalam Microsoft Office Word. Namun, jika Anda memiliki banyak teks yang ingin Anda masukkan dan format, teks itu mungkin memerlukan banyak kode. Jika data dapat direpresentasikan sebagai string Format Teks Kaya (RTF), Anda dapat sering mengurangi kode Otomatisasi. Anda dapat membuat string RTF, menyalin string RTF ke clipboard, lalu menempelkan string RTF ke dalam dokumen.Artikel ini menjelaskan cara menyusun contoh Visual Basic sederhana yang memulai Word, membuat dokumen baru, dan menambahkan beberapa teks berformat ke dokumen dengan menggunakan string RTF bawaan.
Informasi Selengkapnya
Untuk membuat proyek contoh, ikuti langkah-langkah ini:
-
Mulai Visual Basic, lalu buat Standard EXE baru. Secara default, formulir yang dinamai Form1 akan dibuat.
-
Menambahkan Tombol Command ke formulir, klik dua kali Tombol Command, lalu tambahkan kode berikut ke kejadian Klik.
'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
-
Tambahkan kode berikut ke bagian Deklarasi Umum modul Formulir.
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
-
Tekan tombol F5 untuk menjalankan proyek. Word dimulai, lalu dokumen baru dibuat yang berisi teks berformat.
Referensi
Untuk informasi selengkapnya dan untuk sampel untuk pengembangan Office solusi, kunjungi situs Web Microsoft berikut ini:
http://support.microsoft.com/ofd http://msdn.microsoft.com/office