文章編號: 274326 - 上次校閱: 2007年5月24日 - 版次: 5.1

如何使用 Visual Basic 將 HTML 程式碼加入至剪貼簿

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文將告訴您,如何加入和使用 Visual Basic 來擷取 Microsoft Windows 剪貼簿上的 HTML 資料。

其他相關資訊

開發人員可以使用 CF_HTML 剪貼簿格式 (HTML 格式) 與了解如 Microsoft Office 和 Microsoft Internet Explorer 的 HTML 其他應用程式共用 HTML 資料。

CF_HTML 完全是文字為主的格式,包含描述、 一個內容及該內容內的片段。當您正在建置要傳送到剪貼簿資料時,您必須包含以指出剪貼簿版本,且 [內容] 和 [片段位移資料的說明。您可以使用本文稍後所述的 PutHTMLClipboard 程序來簡化這項工作。

建立範例專案的步驟

  1. 在 Visual Basic 中啟動新的標準 EXE 專案。預設會建立 Form1。
  2. 將兩個 指令按鈕 控制項加入至 Form1。將 Command1] 的 [標題] 屬性變更成 讓 HTML,並將 Command2] 的 [標號] 屬性變更來 取得 HTML
  3. 在 [專案] 功能表上, 選擇 [加入模組],以將標準的 BAS 模組加入至專案]。
  4. 將下列程式碼加入至 [模組] 視窗:
    
    Option Explicit
    
    Private Declare Function CloseClipboard Lib "user32" () As Long
    Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) _
       As Long
    Private Declare Function GlobalAlloc Lib "kernel32" ( _
    
       ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function SetClipboardData Lib "user32" ( _
       ByVal wFormat As Long, ByVal hMem As Long) As Long
    Private Declare Function EmptyClipboard Lib "user32" () As Long
    Private Declare Function RegisterClipboardFormat Lib "user32" Alias _
       "RegisterClipboardFormatA" (ByVal lpString As String) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
       As Long
    Private Declare Function GlobalUnlock Lib "kernel32" ( _
       ByVal hMem As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
       pDest As Any, pSource As Any, ByVal cbLength As Long)
    Private Declare Function GetClipboardData Lib "user32" ( _
       ByVal wFormat As Long) As Long
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
       ByVal lpData As Long) As Long
    
    Private Const m_sDescription = _
                      "Version:1.0" & vbCrLf & _
                      "StartHTML:aaaaaaaaaa" & vbCrLf & _
                      "EndHTML:bbbbbbbbbb" & vbCrLf & _
                      "StartFragment:cccccccccc" & vbCrLf & _
                      "EndFragment:dddddddddd" & vbCrLf
                      
    Private m_cfHTMLClipFormat As Long
    
    Function RegisterCF() As Long
    
    
       'Register the HTML clipboard format
       If (m_cfHTMLClipFormat = 0) Then
          m_cfHTMLClipFormat = RegisterClipboardFormat("HTML Format")
       End If
       RegisterCF = m_cfHTMLClipFormat
       
    End Function
    
    Public Sub PutHTMLClipboard(sHtmlFragment As String, _
       Optional sContextStart As String = "<HTML><BODY>", _
       Optional sContextEnd As String = "</BODY></HTML>")
       
       Dim sData As String
       
       If RegisterCF = 0 Then Exit Sub
       
       'Add the starting and ending tags for the HTML fragment
       sContextStart = sContextStart & "<!--StartFragment -->"
       sContextEnd = "<!--EndFragment -->" & sContextEnd
       
       'Build the HTML given the description, the fragment and the context.
       'And, replace the offset place holders in the description with values
       'for the offsets of StartHMTL, EndHTML, StartFragment and EndFragment.
       sData = m_sDescription & sContextStart & sHtmlFragment & sContextEnd
       sData = Replace(sData, "aaaaaaaaaa", _
                       Format(Len(m_sDescription), "0000000000"))
       sData = Replace(sData, "bbbbbbbbbb", Format(Len(sData), "0000000000"))
       sData = Replace(sData, "cccccccccc", Format(Len(m_sDescription & _
                       sContextStart), "0000000000"))
       sData = Replace(sData, "dddddddddd", Format(Len(m_sDescription & _
                       sContextStart & sHtmlFragment), "0000000000"))
    
       'Add the HTML code to the clipboard
       If CBool(OpenClipboard(0)) Then
       
          Dim hMemHandle As Long, lpData As Long
          
          hMemHandle = GlobalAlloc(0, Len(sData) + 10)
          
          If CBool(hMemHandle) Then
                   
             lpData = GlobalLock(hMemHandle)
             If lpData <> 0 Then
                
                CopyMemory ByVal lpData, ByVal sData, Len(sData)
                GlobalUnlock hMemHandle
                EmptyClipboard
                SetClipboardData m_cfHTMLClipFormat, hMemHandle
                            
             End If
          
          End If
       
          Call CloseClipboard
       End If
    
    End Sub
    
    Public Function GetHTMLClipboard() As String
    
       Dim sData As String
       
       If RegisterCF = 0 Then Exit Function
       
       If CBool(OpenClipboard(0)) Then
       
          Dim hMemHandle As Long, lpData As Long
          Dim nClipSize As Long
          
          GlobalUnlock hMemHandle
    
          'Retrieve the data from the clipboard
          hMemHandle = GetClipboardData(m_cfHTMLClipFormat)
          
          If CBool(hMemHandle) Then
                   
             lpData = GlobalLock(hMemHandle)
             If lpData <> 0 Then
                nClipSize = lstrlen(lpData)
                sData = String(nClipSize + 10, 0)
                
    
                Call CopyMemory(ByVal sData, ByVal lpData, nClipSize)
                
                Dim nStartFrag As Long, nEndFrag As Long
                Dim nIndx As Long
                
                'If StartFragment appears in the data's description,
                'then retrieve the offset specified in the description
                'for the start of the fragment. Likewise, if EndFragment
                'appears in the description, then retrieve the
                'corresponding offset.
                nIndx = InStr(sData, "StartFragment:")
                If nIndx Then
                   nStartFrag = CLng(Mid(sData, _
                                     nIndx + Len("StartFragment:"), 10))
    
                End If
                nIndx = InStr(sData, "EndFragment:")
                If nIndx Then
                   nEndFrag = CLng(Mid(sData, nIndx + Len("EndFragment:"), 10))
                End If
                
                'Return the fragment given the starting and ending
                'offsets
                If (nStartFrag > 0 And nEndFrag > 0) Then
                   GetHTMLClipboard = Mid(sData, nStartFrag + 1, _
                                     (nEndFrag - nStartFrag))
                End If
                            
             End If
          
          End If
    
       
          Call CloseClipboard
       End If
    
    
    End Function
    					
  5. 在程式碼視窗中將下列程式碼加入 Form1
    Private Sub Command1_Click()
        Dim sFrag As String, sStart As String, sEnd As String
        sStart = "<HTML><BODY><FONT FACE=Arial SIZE=1 COLOR=BLUE>"
        sFrag = "<B>This is bold</B> and <I>this is italic.</I>"
        sEnd = "</FONT></BODY></HTML>"
        PutHTMLClipboard sFrag, sStart, sEnd
    End Sub
    
    Private Sub Command2_Click()
        MsgBox GetHTMLClipboard
    End Sub
    					
  6. 按下 F5 鍵以啟動 Visual Basic 程式。按一下 [讓 HTML,將 HTML 資料新增到剪貼簿]。然後,按一下 [Get HTML 來擷取剪貼簿資料。請注意訊息方塊會顯示 HTML 片段。
  7. 啟動 Word (或可辨識 HTML 格式] 剪貼簿的任何應用程式)。貼上剪貼簿的內容到新文件中,檢查包含剪貼簿上的 HTML 程式碼的結果。

額外的附註

使用傳送到剪貼簿的 HTML 程式碼的方式可能會為 Office 自動化用戶端特別有用。比方說如果您需要在 Microsoft Excel 或 Microsoft Word 中的段落中產生的儲存格的格式化的資料的自動化用戶端可以建置 HTML 程式碼中的資料、 將它傳送到剪貼簿,然後將其貼至應用程式。利用這項技術您可以減少自動化用戶端的逾時的處理程序呼叫的數目。

?考

若要學習更多有關 HTML 的剪貼簿格式的細節,請參閱主題 HTML 剪貼簿格式,在下列的 Microsoft 開發 o 人 h 員 ? 工 u 具 ? 網路 (MSDN) 的網站]:
http://msdn2.microsoft.com/en-us/library/Aa767917.aspx (http://msdn2.microsoft.com/en-us/library/Aa767917.aspx)

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Office Basic 2007
  • Microsoft Office Enterprise 2007
  • Microsoft Office Home and Student 2007
  • Microsoft Office Professional 2007
  • Microsoft Office Professional Plus 2007
  • Microsoft Office Small Business 2007
  • Microsoft Office Standard 2007
  • Microsoft Office Ultimate 2007
  • Microsoft Office Professional Edition 2003
  • Microsoft Office Basic Edition 2003
  • Microsoft Office Small Business Edition 2003
  • Microsoft Office Standard Edition 2003
  • Microsoft Office Students and Teachers Edition 2003
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
關鍵字:?
kbmt kbexpertiseinter kbclipboard kbhowto kbhtml KB274326 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:274326? (http://support.microsoft.com/kb/274326/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。