PDA

View Full Version : Is Clipboard entry a Table?



fionabolt
03-05-2008, 08:56 AM
The following link to an MVP's site tells us how to get the string from the clipboard:
http://word.mvps.org/faqs/MacrosVBA/ManipulateClipboard.htm

Does anyone know how I can further manipulate this.

Ideally I want to know if the content of the clipboard is a table. If it is a table, I then want to insert it into the document with specific formatting.

Thanks
F

fumei
03-05-2008, 11:27 AM
The article uses a dataobject. The DataObject currently supports only text formats. So, no you can not. Clipboard contents (if that includes a table) put into a DataObject will NOT, repeat not, pass that table. If you insert the contents of the DataObject, all text in the table will be there...but the table will NOT. Note that the change in cells will, by default, become Tabs.

The Clipboard however, can contain any of the following data types:

wdPasteBitmap
wdPasteDeviceIndependentBitmap
wdPasteEnhancedMetafile
wdPasteHTML
wdPasteHyperlink
wdPasteMetafilePicture
wdPasteOLEObject
wdPasteRTF
wdPasteShape
wdPasteText


but contains a table is not one of them.

Unfotunately, you can not test the contents of the clipboard to see if contains a table. I say unfortunately because it would be nice if you could, but also because you can get an error if you use PasteAppendTable.

From Help: You can use PasteAsNestedTable only if the Clipboard contains a cell or group of cells.

Well that is just peachy, but if the Clipboard does NOT contain a cell or groups of cells, you get a 4605 error "This command is not available."

Yeah...well yipppeee!

You could gludge away and paste, then select the paste from the document, and THEN check if it contains a table. or you could trap error 4605, but....IMO that is not explicit enough. Who knows if you could be getting the error for something else?

TonyJollans
03-05-2008, 11:31 AM
Hi Fiona,

You do ask some awkward questions :)

When data is copied to the clipboard it is put there in several different formats (the ones you see in the Paste Special selection) depending on the application that puts it there. If the application that put the data there is still running, some of those formats may actually just be pointers to the application, saying to contact it if the data are requested to be pasted in that format (this is part of the reason Word sometimes asks on closing if you want to keep data on the clipboard - as it will no longer be running it will no longer be available to be asked for the data in some formats - and if you say you want to keep the data one of two things happens with each affected format: the full data in the format is physically copied to the clipboard, or the reference to the format is removed from the clipboard).

The data on the clipboard could be anything - text, formatted text, picture(s), file(s), anything. if it is formatted text, it may be, or may contain, one or more tables or, more accurately perhaps, arrangements of data that will be interpreted by Word as tables. In general tabular data are available as HTML on the clipboard. Such stuff is not available to VBA and the best you might reasonably be able to do would be to temporarily paste it to a document and see if the result contained tables.

I hope some of that was interesting; it is, however, probably of little use to you :) I'm not sure what you gain by knowing in advance that you want to do some formatting afterwards. Why not check the pasted Range and see if it contains tables and, if so, then do your stuff.

TonyJollans
03-05-2008, 11:42 AM
Well, Gerry has (a) beaten me to it and (b) answered my question about what you gain by knowing in advance what data are there.

I think the only thing you can do is paste temporarily somewhere else (within Word) and examine the data.

fionabolt
03-06-2008, 03:37 AM
Thanks to both of you for your replies.

I agree that it would be possible to paste the output, and then check for the instance of a table, but that seems overly clunky!

Tony is correct that where there is a table in the clipboard item, then the data is HTML format. As it is HTML format, is in not possible to query the clipboard item for "<TABLE>" .

At the moment I'm looking into the GetClipboard API instead. Please let me know if I'm barking up totally the wrong tree.

Thanks
F

TonyJollans
03-06-2008, 10:02 AM
I think you may have the right tree - I refuse to say whether you're barking or not :)

GetClipboardData should be able to give you a handle to the HTML format (if it exists). I've never done it so don't know, but presume it will just be a text stream of the HTML (something like the HTMLBody of an e-mail in Outlook, I guess).

fumei
03-06-2008, 11:23 AM
It is certainly a tree, but unless you have a serious reason to get involved with API, IMO getting the ladder to reach the branch is not worth the effort.

Have looked at what is involved with using GetClipboardData?

Sure, if you want to learn using API it may be worth your time, but as a business case just to know if the clipboard contents have a table, or not, it is overkill to the extreme.
Function HasTable() As Boolean
Documents.Add
Selection.Paste
If ActiveDocument.Content.Tables.Count > 0 Then
HasTable = True
Else
HasTable = False
End If
ActiveDocument.Close wdDoNotSaveChanges
End Function

Sub Yadda()
MsgBox HasTable
End Sub

The function returns a True or False regarding if there is a table in the clipboard. Make a new document, dump in the clipboard, check to see if there is table (or more) and return yes or no, get rid of new document.

Done.

Of course you could add further instructions like:Sub Yadda()
If HasTable Then
' insert the clipboard contents
' do whatever with the table
Else
' whatever you do if clipboard
' does NOT have a table
End If
End Sub

Note that the function, as it stands, only detects if the clipboard contents table count is non-zero.



text text text
text text text
<table>
text text text
text text text

returns True

<table> i.e. ONLY a table, will also return True.

If you require to know if the clipboard is text AND a table, rather than ONLY a table, you would have to expand the logic.