Microsoft KB Archive/274308

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

Article ID: 274308

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual C++ 6.0 Professional Edition

-



This article was previously published under Q274308



SUMMARY
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(&quot;This is a test&quot;);

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(&quot;HTML Format&quot;);

// Create a template string for the HTML header... strcpy(buf,       &quot;Version:0.9\r\n&quot;        &quot;StartHTML:00000000\r\n&quot;        &quot;EndHTML:00000000\r\n&quot;        &quot;StartFragment:00000000\r\n&quot;        &quot;EndFragment:00000000\r\n&quot;        &quot;  \r\n&quot;        &quot;\r\n&quot;);

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

// 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, &quot;StartHTML&quot;); wsprintf(ptr+10, &quot;%08u&quot;, strstr(buf, &quot; &quot;) - buf); *(ptr+10+8) = '\r';

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

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

ptr = strstr(buf, &quot;EndFragment&quot;); wsprintf(ptr+12, &quot;%08u&quot;, strstr(buf, &quot;<!--EndFrag&quot;) - 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... EmptyClipboard; // 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); GlobalUnlock(hText); ::SetClipboardData(cfid, hText); CloseClipboard; // Free memory... GlobalFree(hText); }

// 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 = &quot;This is a test &quot; &quot;entry 1&quot; &quot;entry 2&quot;; CopyHTML(html);

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.

