How to place text on the clipboard so that it is pasted as a table in Word?

Using VBA in MS Office, how to add text to the Windows clipboard so that it is pasted into Word as a table?

+6
clipboard ms-word word-vba ms-office
source share
5 answers

The Windows clipboard supports several formats. When you want to put things on the clipboard, you make one or more calls to RegisterClipboardFormat (), telling it the formats of the objects that you will place on the clipboard, and then calls to SetClipboardData (), which actually put the data on the clipboard.

If you want to be able to embed a table in Word, then HTML is the easiest format to work with. Just copy the HTML table to the clipboard and paste it correctly in Word, provided that you first register the clipboard data as an HTML object.

I would give you the code, but the easiest way is to simply reference the example on MSDN:

How to add HTML code to clipboard using Visual Basic

This page even shows an example of copying an HTML table to the clipboard.

+6
source share

Kluge's answer is correct, but there is better code in this other MSDN article that is easier to reuse: http://blogs.msdn.com/jmstall/archive/2007/01/21/html-clipboard.aspx

+4
source share

Have you tried formatting it as an HTML table?

0
source share

It has been some time since I did any programming on Windows, but it seems that I remember that you are registering the format of the object. In fact, you can register several objects of different formats, and the paste application can choose between them (for example, using the Word Paste Special option).

I would try to create a Word table object, fill it with cells with your data, and then copy them to the clipboard.

0
source share

The clipboard is wonderful ... But something seems a bit sketchy in using it to store output from your program for pasting into Word. What are you trying to do, what are you likely to do to others?

0
source share

All Articles