How To Add HTML Code to the Clipboard by Using Visual C++


This article demonstrates how to add Hypertext Markup Language (HTML) to the Microsoft Windows clipboard by using Visual C++. It also includes a sample function that you can use in your applications to simplify the process.

More Information

HTML has its own clipboard format called HTML Format (CF_HTML) that you can use to provide your data to other applications, such as Microsoft Excel, Microsoft Word, or other Microsoft Office applications.

CF_HTML is entirely a text-based format that includes a description, a context, and a fragment within that context. When you build 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. Calculating the offsets can be the difficult part. However, you can use the following routine to simplify this task:

// CopyHtml() - Copies given HTML to the clipboard.
// The HTML/BODY blanket is provided, so you only need to
// call it like CallHtml("<b>This is a test</b>");

void CopyHTML(char *html) {
// Create temporary buffer for HTML header...
char *buf = new char [400 + strlen(html)];
if(!buf) return;

// Get clipboard id for HTML format...
static int cfid = 0;
if(!cfid) cfid = RegisterClipboardFormat("HTML Format");

// Create a template string for the HTML header...
"<!--StartFragment -->\r\n");

// Append the HTML...
strcat(buf, html);
strcat(buf, "\r\n");
// Finish up the HTML format...

// Now go back, calculate all the lengths, and write out the
// necessary header information. Note, wsprintf() truncates the
// string when you overwrite it so you follow up with code to replace
// the 0 appended at the end with a '\r'...
char *ptr = strstr(buf, "StartHTML");
wsprintf(ptr+10, "%08u", strstr(buf, "<html>") - buf);
*(ptr+10+8) = '\r';

ptr = strstr(buf, "EndHTML");
wsprintf(ptr+8, "%08u", strlen(buf));
*(ptr+8+8) = '\r';

ptr = strstr(buf, "StartFragment");
wsprintf(ptr+14, "%08u", strstr(buf, "<!--StartFrag") - buf);
*(ptr+14+8) = '\r';

ptr = strstr(buf, "EndFragment");
wsprintf(ptr+12, "%08u", strstr(buf, "<!--EndFrag") - buf);
*(ptr+12+8) = '\r';

// Now you have everything in place ready to put on the
// clipboard.

// Open the clipboard...
if(OpenClipboard(0)) {

// Empty what's in there...

// Allocate global memory for transfer...
HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4);

// Put your string in the global memory...
char *ptr = (char *)GlobalLock(hText);
strcpy(ptr, buf);

::SetClipboardData(cfid, hText);

// Free memory...


// Clean up...
delete [] buf;

When you use this function to copy an HTML code fragment to the clipboard, it might look like the following:

    char *html = 
"<b>This is a test</b><hr>"
"<li>entry 1"
"<li>entry 2";

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.


To learn more about the specifics of the HTML Clipboard format, see the topic "HTML Clipboard Format" in the MSDN:

Artikelnummer: 274308 – Letzte Überarbeitung: 19.06.2014 – Revision: 1