สรุป
ด้วย Automation คุณสามารถเกือบทุกอย่างทางโปรแกรมที่ผู้ใช้สามารถกระMicrosoft Officeใน Word อย่างไรก็ตาม ถ้าคุณมีข้อความหลายข้อความที่คุณต้องการใส่และจัดรูปแบบ อาจต้องใช้โค้ดหลายรหัส ถ้าคุณสามารถแสดงข้อมูลเป็นสตริง Rich Text Format (RTF) คุณสามารถลดโค้ด Automation บ่อยครั้งได้ คุณสามารถสร้างสตริง RTF คัดลอกสตริง RTF ไปยังคลิปบอร์ด แล้ววางสตริง RTF ลงในเอกสาร
บทความนี้จะอธิบายถึงวิธีการสร้างตัวอย่างVisual Basicที่เริ่ม Word สร้างเอกสารใหม่ และเพิ่มข้อความที่จัดรูปแบบบางอย่างลงในเอกสารโดยใช้สตริง RTF ที่สร้างไว้ล่วงหน้า
ข้อมูลเพิ่มเติม
เมื่อต้องการสร้างโครงการตัวอย่าง ให้ปฏิบัติตามขั้นตอนเหล่านี้:
-
เริ่ม Visual Basicใหม่ แล้วสร้าง EXE มาตรฐานใหม่ ตามค่าเริ่มต้น ฟอร์มที่มีชื่อ Form1 จะถูกสร้างขึ้น
-
เพิ่มปุ่ม Command ลงในฟอร์ม ดับเบิลคลิก ปุ่ม Command แล้วเพิ่มโค้ดต่อไปนี้ลงในเหตุการณ์ Click
'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 -
เพิ่มโค้ดต่อไปนี้ลงในส่วนการประกาศทั่วไปของมอดูลฟอร์ม
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 -
กดแป้น F5 เพื่อเรียกใช้โครงการ Word จะเริ่มขึ้น แล้วเอกสารใหม่จะถูกสร้างขึ้นซึ่งมีข้อความที่จัดรูปแบบ
อ้างอิง
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