How to add HTML code to the clipboard by using Visual Basic

Summary

This article demonstrates how to add and retrieve HTML data on the Microsoft Windows clipboard by using Visual Basic.

More Information

Developers can use the CF_HTML clipboard format (HTML Format) to share HTML data with other applications that understand HTML, such as Microsoft Office and Microsoft Internet Explorer.

CF_HTML is entirely a text-based format that includes a description, a context, and a fragment within that context. When you are building data to send to the clipboard, you must include a description of the data to indicate the clipboard version and the offsets for the context and fragment. You can use the PutHTMLClipboard procedure described later in this article to simplify this task.

Steps to create a sample project

  1. Start a new Standard EXE Project in Visual Basic. Form1 is created by default.
  2. Add two CommandButton controls to Form1. Change the Caption property of Command1 to Put HTML, and change the Caption property of Command2 to Get HTML.
  3. On the Project menu, choose Add Module to add a standard BAS module to the project.
  4. Add the following code to the module window:
    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. Add the following code in the code window for 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. Press the F5 key to start the Visual Basic program. Click Put HTML to add the HTML data to the clipboard. Then, click Get HTML to retrieve the clipboard data. Note that a message box displays the HTML fragment.
  7. Start Microsoft Word (or any application that recognizes the HTML Format for the clipboard). Paste the clipboard contents to a new document to examine the results of the HTML code contained on the clipboard.

Additional note

Using an approach that sends HTML code to the clipboard might be especially beneficial for Office Automation clients. For example, if you have an Automation client that needs to generate formatted data for cells in Microsoft Excel or paragraphs in Microsoft Word, you could build the data in HTML code, send it to the clipboard, and then paste it into the application. By using this technique, you could reduce the number of out-of-process calls to the Automation client.

References

To learn more about the specifics of the HTML clipboard format, see the topic "HTML Clipboard Format" at the following Microsoft Developer Network (MSDN) Web site:
Properties

Article ID: 274326 - Last Review: Mar 23, 2009 - Revision: 1

Feedback