2
votes

I have 2 applications: one is based on a WPF DataGrid and the other is an Excel AddIn.

My goal is to pass data, via Clipboard, from the former to the latter.

I have no problem copying tab-separated, cuadriculated data into the regular Excel "Paste" operation. Additionally, I implemented my own "Smart Paste".

My trouble began when I decided to copy+paste a more complex object. I marked all my classes as [Serializable] and have a method that makes sure that the object can be serialized. In fact, the whole operation works perfectly provided that the pasting app is NOT the Excel AddIn.

The passed object is of a class named Engineering.Export.Exported. As we can see, the clipboard looks fine:

http://patriot.net/~ramon/misc/Copy-is-Okay.png

See the 3 relevant statements. They are identical in both applications.

http://patriot.net/~ramon/misc/Failed-Assignment.png

The last assignment fails only in the application where I need it.

I have tried many combinations, for instance GetData("ExportFormat") but the result is the same.

Any tips, suggestions are very welcome.

TIA

Addition:

I prepared a super-simplistic object:

namespace Engineering
{
    [Serializable]
    public class SimpleStuff
    {
        public string Greetings;
        public int GraduationYear;
        public bool Available;
    }
}

and the problem remains: I can export it into any application except a VSTO AddIn. The last statement always returns null, inside AddIns.

2
How are you telling the addin how your type Exported (or the test case SimpleStuff) is defined? Does it link to the actual library where the type is defined? Some other mechanism e.g. a COM type library? - Eric J.
Eric: I simply declared both identically, at the source code level. - oleworldcoder
My question remains: HAS ANYBODY BEEN ABLE TO DO THIS? - oleworldcoder
Eric: Your comment made me think. What if company "A" implements the copied application, while company "B" implements the pasted on application? It makes sense that they agree at the source code level, without having to perform some common linking. - oleworldcoder
I have not worked with the clipboard for a long time... can you get the clipboard data as binary data and check that it is exactly as expected, rather than directly deserializing from the clipboard? - Eric J.

2 Answers

1
votes

If you are good at debugging, you could "test" what the type is:

private void pasteButton_Click(object sender, RoutedEventArgs e)
{
    var dataObject = Clipboard.GetDataObject();
    if (dataObject != null) {
        Exported incomingObject;
        if (dataObject.GetDataPresent(typeof(Exported))) {
            incomingObject = dataObject.GetData(typeof(Exported));
        } else {
            var obj = dataObject.GetData(typeof(Object));
            incomingObject = obj as Exported;
        }
        if (incomingObject != null) {
          // what you want to do with it can go here
        }
    }
}

Put a breakpoint on Console.WriteLine and see what the dataType variable is.

Naturally, make sure you are only testing with the data types you are interested in.

Let me know how that goes.

0
votes

After running some experiments with a non-AddIn application, I found the problem. Instead of returning null, the statement in question fails with an error message about a missing assembly.

The solution is to create a separate MSVS Project that contains only the object to be transferred, compile it and add a Reference to it, in the two applications involved.

Thanks to Eric J.