I have a Excel addin written in C# that has some Copy/Paste processing code. When the user copies, I want to put my own TableData class object on the clipboard and retrieve it back when the user paste the data on the worksheet. Sounds real simple and I found a dozen articles online but for some reason it is not working in my code.
Here is my TableData class:
[Serializable]
[Guid("794E06D9-7CE4-40dc-8187-BDC105A0F866")]
public class TableData
{
public string Name;
public TableData()
{ }
}
I even gave it a GUID to make sure its loading the exact same class even if it is loading from some other assembly (if addin object loaded twice for some reason).
This is what I am doing to set data while copying:
TableData data = new TableData();
data.Name = "test";
Clipboard.SetData("MyTable", data);
And on Paste:
TableData data = Clipboard.GetData("MyTable") as TableData;
HERE >> data is null but if I print the GetData("MyTable") in immediate window, it shows a memory stream which I was unable to convert to TableData even by using the BinaryFormatter::DeSerialize() method
I might be doing some silly mistake here but I am unable to figure it out. Can somebody point out the problem here?
UPDATED
It is a COM addin and NOT a VSTO addin. I have set the macros in OnKey method so they get called when the user presses CTRL+C and CTRL+V in Excel worksheet. There is a separate VBA module that handles these requests and routes them to the .NET addin.
Application.OnKey("^c", "OnCopy");
So the call goes to VBA and then the VBA calls back into my .NET addin. Not sure if there is some other process involved in between the calls.
Works fine if I put and retrieve byte[] from the clipboard but not for my custom class