How can I tell if clipboard contents can be pasted as Enhanced Metafile

I am developing an add-in for Word in VB.NET. One of the functions is to Paste clipboard contents as an Enhanced Metafile (my company frequently copy charts and tables from Excel to Word in this format).

I would like to test that the contents of the clipboard can be pasted as enhanced metafile. Currently I just trap the error if it cannot but if I can find out in advance I can then disable the menu item (or change its name) to be clearer to the user.

Code for this test would be much appreciated.
Who is Participating?
Fernando SotoConnect With a Mentor RetiredCommented:
Hi CarlPritchard;

Here is sample code to show you how to see if the clipboard has the correct data. No special imports needed.

        If Clipboard.ContainsData(DataFormats.EnhancedMetafile) Then
            ' Then the Clipboard contains data in the EnhancedMetafile format
            ' The Clipboard DOES NOT contain data in the EnhancedMetafile
        End If

Open in new window

athapaCEO / CTOCommented:
Use ClipBoard.GetDataPresent to figure out if the format you want is available in the clipboard. You can also enumerate all of the formats available in the clipboard.

CarlPritchardAuthor Commented:
Please could you give a code example that shows if there is data that can be pasted as enhanced metafile (ie is compatible data) including any required namespaces/imports.
athapaCEO / CTOCommented:
the only required name space is
which should be already there if your app is windows form app.

imports System.Windows.Form

Private Const FormatName As String = "EnhancedMetafile" 'Enhanced Meta-File Picture

'Call this function anytime you want to see if EnhancedMetaFile is in the clipboard
Private Function IsEnhancedMetaAvailableInClipboard() As Boolean
        Dim clip As IDataObject = Clipboard.GetDataObject()
        Dim AvailableFormats As String() = clip.GetFormats()

        Return (System.Array.IndexOf(AvailableFormats, FormatName) > -1)

End Function

I used the following test code to figure out what other formats are available in clipboard as well as to test the function above

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        For Each ClipboardFormat As String In Clipboard.GetDataObject().GetFormats()

    End Sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.