Clipboard opening failed

3

I am having a problem regarding clipboard and I get this error message every time I try to make a copy / paste operation from an Excel file.

The code breaks at Clipboard.GetDataObject() and the message error is this:

OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))

My WPF application is already running when I open Excel, write some text and then try to copy / paste.

The code that I'm using is this:

private void SetClipboardData()
{
    IDataObject data = Clipboard.GetDataObject();
    IList result = GetDataForFileDropFormat( data );

    if ( ( result != null ) && ( result.Count > 0 ) )
    {
        this._elementsClipboard = result;
        this._sourceDrag = null;
        this._sourceClipboard = null;
    }
}

The above function is called in this handler method:

public void Handle_WM_DRAWCLIPBOARD( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
{
    SendMessage( this._nextClipboardViewer, msg, wParam, lParam );

    // get data from clipboard
    SetClipboardData();

    handled = true;
}

I have been searching MSDN and .NET forums, but didn't find a workaround for this issue.

Could someone help me with that or give me an idea of what should I try?

Thanks!

wpf
clipboard
clipboarddata
asked on Stack Overflow Feb 1, 2012 by Costinel Dumitru • edited Feb 2, 2012 by Samuel Slade

2 Answers

3

A couple of issues here. Fist, while you definitely DO need to send the WM_DrawClipboard to the next app in the chain, you don't need to do it FIRST. You can do it after you run your own stuff, THEN pass the message along.

Next, don't expect Excel to perform all of its clipboard updating in one operation. I have seen Excel perform as many as 24 updates in a row, when copying complex objects. (particularly graphs - they'd open/close the clipboard after adding each individual format).

Also, Excel takes advantage of Delayed Rendering for almost everything besides plain text. So while your app is requesting data, Excel has render it. That can take time.

You may need to implement a delay with a "while not success or 3-strikes loop". You'll need to pay close attention to whether you send the WM_DrawClipboard down the chain before or after your processing though, as you may be setting up another collision with other clipboard viewers who are also interested in Excel data, and have to resort to such tricks themselves.

And you thought this would be easy.....

answered on Stack Overflow Feb 3, 2012 by Chris Thornton
0

Don't you need to check if the Clipboard object contains the right kind of data and request the object of that type? It can hold multiple objects of different types and maybe you're getting an item of an unexpected type. I use something like the following, although clearly in your case, you won't be requesting my custom Address object from the Clipboard.

if (System.Windows.Clipboard.ContainsData("Address"))
{
    try
    {
        return (SerializableAddress)System.Windows.Clipboard.GetData("Address");
    }
    catch (COMException)
    {
        return null; 
    }
}
return null;
answered on Stack Overflow Feb 4, 2012 by Sheridan

User contributions licensed under CC BY-SA 3.0