摘要

有了自動化,您幾乎可以在 Word 中以程式設計方式執行任何Microsoft Office操作。 不過,如果您有許多要輸入及格式化的文字,可能需要許多程式碼。 如果您可以將資料以 RTF 字串 (RTF) RTF 格式,您可以經常減少自動化程式碼。 您可以建立 RTF 字串、將 RTF 字串複製到剪貼簿,然後將 RTF 字串貼到檔中。

本文將說明如何使用預先Visual Basic RTF 字串來建立簡單的文字範例,以啟動 Word、建立新檔,以及新增一些格式化文字至檔。

其他相關資訊

若要建立範例專案,請遵循下列步驟:

  1. 開始Visual Basic,然後建立新的標準 EXE。 根據預設,會建立名為 Form1 的表單。

  2. 在表單中新增 CommandButton,按兩下 CommandButton,然後將下列程式碼新增到 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

  3. 在表單模組的一般宣告區段新增下列程式碼。

    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
  4. 按 F5 鍵以執行專案。 Word 隨即啟動,然後會建立一份包含格式化文字的新檔。

參考

有關開發解決方案Office範例,請流覽下列 Microsoft 網站:



HTTP://support.microsoft.com/ofdHTTP://msdn.microsoft.com/office

需要更多協助?

擴展您的技能
探索訓練
優先取得新功能
加入 Microsoft 測試人員

這項資訊有幫助嗎?

您對語言品質的滿意度如何?
以下何者是您會在意的事項?

感謝您的意見反應!

×