Using VBA in MS Office, how do I add text to the Windows clipboard so that it will paste into Word as a table?
4 Answers
The Windows clipboard supports multiple formats. When you want to place things in the clipboard, you make one or more calls to RegisterClipboardFormat() telling it the formats of the objects you will be placing on the clipboard, followed by calls to SetClipboardData() which actually places the data into the clipboard.
If you want to be able to paste a Table into Word, then HTML is the easiest format to work with. Just copy an HTML table onto the clipboard, and it will paste correctly into Word, provided that you first register the clipboard data as an HTML object.
I'd give you some code, but it's easiest to just reference an example on MSDN:
How to add HTML code to the clipboard using Visual Basic
This page even shows an example of copying an HTML table onto the clipboard.

- 3,567
- 3
- 24
- 21
-
I got this working, but found that I can't paste table cell data into an existing Word table, in the same way you can with copy-and-paste between Word tables. Instead, the clipboard table data shows up as a nested table within a single Word table cell. Getting that working probably requires a detailed understanding of what Word expects to find on the clipboard. – Craig McQueen Nov 12 '10 at 03:17
-
To paste table cells into Word tables the way Word does, I'm guessing that I'd need to put RTF format onto the clipboard. – Craig McQueen Nov 12 '10 at 03:41
Have you tried formatting it as a HTML table?

- 4,469
- 23
- 30
-
Doesn't work. You need to tell the clipboard it's an HTML table when you copy it for that to work. – Chris B. Nov 17 '08 at 22:53
It's been a while since I've done any Windows programming, but I seem to recall that you register the format of the object. In fact, you can register multiple objects of different formats and the pasting application can choose between them (such as with Word's Paste Special
option).
I would try to create a Word table object, fill it's cells with your data and then copy that onto the clipboard.

- 29,935
- 4
- 60
- 73
The Clipboard is great... But something seems a little sketchy about using it to hold the output from your program to paste into Word. What is it that you're trying to do that you probably should be doing some other way?