Microsoft KB Archive/274326

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

Article ID: 274326

Article Last Modified on 5/24/2007

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * 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 Student and Teacher Edition 2003
 * Microsoft Office XP Developer Edition
 * Microsoft Office 2000 Developer Edition

-



This article was previously published under Q274326



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
 Start a new Standard EXE Project in Visual Basic. Form1 is created by default. 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. On the Project menu, choose Add Module to add a standard BAS module to the project.  Add the following code to the module window: Option Explicit

Private Declare Function CloseClipboard Lib &quot;user32&quot; As Long Private Declare Function OpenClipboard Lib &quot;user32&quot; (ByVal hWnd As Long) _ As Long Private Declare Function GlobalAlloc Lib &quot;kernel32&quot; ( _

ByVal wFlags As Long, ByVal dwBytes As Long) As Long Private Declare Function SetClipboardData Lib &quot;user32&quot; ( _ ByVal wFormat As Long, ByVal hMem As Long) As Long Private Declare Function EmptyClipboard Lib &quot;user32&quot; As Long Private Declare Function RegisterClipboardFormat Lib &quot;user32&quot; Alias _   &quot;RegisterClipboardFormatA&quot; (ByVal lpString As String) As Long Private Declare Function GlobalLock Lib &quot;kernel32&quot; (ByVal hMem As Long) _   As Long Private Declare Function GlobalUnlock Lib &quot;kernel32&quot; ( _ ByVal hMem As Long) As Long Private Declare Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; ( _ pDest As Any, pSource As Any, ByVal cbLength As Long) Private Declare Function GetClipboardData Lib &quot;user32&quot; ( _ ByVal wFormat As Long) As Long Private Declare Function lstrlen Lib &quot;kernel32&quot; Alias &quot;lstrlenA&quot; ( _ ByVal lpData As Long) As Long

Private Const m_sDescription = _ &quot;Version:1.0&quot; & vbCrLf & _ &quot;StartHTML:aaaaaaaaaa&quot; & vbCrLf & _ &quot;EndHTML:bbbbbbbbbb&quot; & vbCrLf & _ &quot;StartFragment:cccccccccc&quot; & vbCrLf & _ &quot;EndFragment:dddddddddd&quot; & vbCrLf Private m_cfHTMLClipFormat As Long

Function RegisterCF As Long

'Register the HTML clipboard format If (m_cfHTMLClipFormat = 0) Then m_cfHTMLClipFormat = RegisterClipboardFormat(&quot;HTML Format&quot;) End If  RegisterCF = m_cfHTMLClipFormat End Function

Public Sub PutHTMLClipboard(sHtmlFragment As String, _  Optional sContextStart As String = &quot;&quot;, _   Optional sContextEnd As String = &quot;&quot;) Dim sData As String If RegisterCF = 0 Then Exit Sub 'Add the starting and ending tags for the HTML fragment sContextStart = sContextStart & &quot;&quot; sContextEnd = &quot;&quot; & 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, &quot;aaaaaaaaaa&quot;, _                  Format(Len(m_sDescription), &quot;0000000000&quot;)) sData = Replace(sData, &quot;bbbbbbbbbb&quot;, Format(Len(sData), &quot;0000000000&quot;)) sData = Replace(sData, &quot;cccccccccc&quot;, Format(Len(m_sDescription & _                  sContextStart), &quot;0000000000&quot;)) sData = Replace(sData, &quot;dddddddddd&quot;, Format(Len(m_sDescription & _                  sContextStart & sHtmlFragment), &quot;0000000000&quot;))

'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, &quot;StartFragment:&quot;) If nIndx Then nStartFrag = CLng(Mid(sData, _ nIndx + Len(&quot;StartFragment:&quot;), 10))

End If           nIndx = InStr(sData, &quot;EndFragment:&quot;) If nIndx Then nEndFrag = CLng(Mid(sData, nIndx + Len(&quot;EndFragment:&quot;), 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   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 = &quot;<FONT FACE=Arial SIZE=1 COLOR=BLUE>&quot; sFrag = &quot;<B>This is bold</B> and <I>this is italic.</I>&quot; sEnd = &quot;</FONT></BODY></HTML>&quot; PutHTMLClipboard sFrag, sStart, sEnd End Sub

Private Sub Command2_Click MsgBox GetHTMLClipboard End Sub </li> 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.</li> 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.</li></ol>

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.

<div class="references_section">