Help and Support
 

powered byLive Search

How To Add Custom Table to the Clipboard to be Pasted into Word

Article ID:216676
Last Review:June 29, 2004
Revision:3.1
This article was previously published under Q216676

SUMMARY

This article shows how to place a custom table on the clipboard so that it is recognized by Microsoft Word as a table when pasting into a document.

Back to the top

MORE INFORMATION

When pasting text into a document, Microsoft Word does not recognize common table formats such as tab-delimited text and CSV (Comma Separated Values). However, it does recognize Rich Text Format (RTF). By building your custom table in RTF and adding it to the clipboard, you can then paste the information into Word as a properly formatted table.

The following code demonstrates how to add a table to the clipboard in both RTF and tab-delimited text so it can be pasted into Microsoft Word and/or Microsoft Excel:

// Open the clipboard...
if(!OpenClipboard()) {
   ::MessageBox(NULL, "Cannot open clipboard.", "Error", 0x10010);
   return;
}

// Get Clipboard format id for RTF.
UINT cf = RegisterClipboardFormat("Rich Text Format");

// Empty anything already there...
EmptyClipboard();

// *** This section of code adds the RTF table to the clipboard
// *** RTF-Data to put on clipboard
const char *text =
   "{\\rtf1\\par "
   "\\trowd \\trgaph30\\trleft-30\\trrh262\\cellx980\\cellx1991\\cellx3001"
   "\\intbl \\qr \\f0\\fs20 \\cf 1\\cell \\qr 2\\cell\\qr 3\\cell \\intbl \\row"
   "\\trowd \\trgaph30\\trleft-30\\trrh262\\cellx980\\cellx1991\\cellx3001"
   "\\intbl \\qr \\f0\\fs20 \\cf 4\\cell \\qr 5\\cell\\qr 6\\cell \\intbl \\row}";

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

// Put our string in the global memory...
char *ptr = (char *)GlobalLock(hText);
strcpy(ptr, text);
GlobalUnlock(hText);

// Put data on the clipboard!
::SetClipboardData(cf, hText);

// Free memory...
GlobalFree(hText);

// *** Now, add a version of the same data as tab-delimited text...
// *** This will be used by Microsoft Excel
char *text2 = "1\t2\t3\n4\t5\t6";

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

// Put our string in the global memory...
ptr = (char *)GlobalLock(hText);
strcpy(ptr, text2);
GlobalUnlock(hText);

// Put data on the clipboard as CF_TEXT
::SetClipboardData(CF_TEXT, hText);

// Free memory...
GlobalFree(hText);


// Close clipboard...
CloseClipboard();
				

Back to the top

REFERENCES

For more information on using RTF, please see the RTF specification in the MSDN under "Rich Text Format (RTF) Specification and Sample RTF Reader Program."

Please be aware that Microsoft does not provide technical support for the RTF reader code or the RTF specification. If you have trouble creating RTF syntax, you can always use Microsoft Word to create some examples for you. Just create a Microsoft Word document with the type of data you are interested in, save it as RTF and open it in Notepad.

Back to the top


APPLIES TO
Microsoft Office XP Developer Edition
Microsoft Office 2000 Developer Edition
Microsoft Office Word 2003
Microsoft Word 2002 Standard Edition
Microsoft Word 2000 Standard Edition
Microsoft Word 97 Standard Edition
Microsoft Visual C++ 5.0 Enterprise Edition
Microsoft Visual C++ 6.0 Enterprise Edition
Microsoft Visual C++ 5.0 Professional Edition
Microsoft Visual C++ 6.0 Professional Edition
Microsoft Visual C++, 32-bit Learning Edition 6.0

Back to the top

Keywords: 
kbhowto kbclipboard KB216676

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by E-mail, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.