How to save and restore the clipboard?

S

Steve

Hello,

I've written a C#-based Outlook COM Add-In that uses custom images for
its buttons. The only mechanism I've found for setting a custom
button image is to paste it from the clipboard with PasteFace. Since
a common user scenario is to copy some HTML onto the clipboard, open a
new email, then paste it, I need to ensure that I save and restore the
HTML content so that the user is unaware and unaffected by the fact I
had to use the clipboard for the button images.

Below is my attempt at doing so. Unfortunately it does not work. It
works fine for saving/restoring plain text, but does not work for
HTML. Does anyone see a problem with my save/restore code or know of
how to accomplish this?

Thanks,
Steve

public void SetButtonImage(string imageFilename)
{
try
{
// save/restore data (in all of its formats) on the clipboard; this
is important so that we don't muck
// with the user's intention (for example, of pasting something into
a new email they create)
IDataObject ido = Clipboard.GetDataObject();
string[] formats = ido.GetFormats(true);
object[] savedDatam = new object[formats.Length];
int i = 0;
foreach (string format in formats)
{
//Trace.WriteLine(format);
savedDatam[i++] = ido.GetData(format);
}
string imagePathname = string.Format(@"{0}\{1}", ImagesDirPath,
imageFilename);
Clipboard.SetDataObject(Image.FromFile(imagePathname));
Trace.Assert(this.button != null);
this.button.PasteFace();
foreach (object savedData in savedDatam)
Clipboard.SetDataObject(savedData);
}
catch (Exception e)
{
Trace.WriteLine(e);
}
}
}
 
S

Steve

Hi,

Ok, so I found a problem with my original code and I've progressed
further. Now, a user can copy HTML to the clipboard (say from an
email), open a new email, and paste it with no problem (i.e., the code
below correctly saves/restores the clipboard contents).

However, there are still problems. If you try to copy an Outlook
email to the clipboard, opening a new task, and pasting the email into
the task's body, it copies a small portion of the email header and not
the Outlook email. Something that may be related to this is that the
_order_ of the formats in the DataObject are _different_ when
restoring the data to the clipboard. More specifically, the order for
the data formats of the DataObject returned from GetFormats() is:
RenPrivateSourceFolder, RenPrivateMessages, FileGroupDescriptor,
FileContents, Object Descriptor, System.String, UnicodeText, and Text.
However, calling GetFormats() on the new DataObject() created in the
code below results in the order: System.String, UnicodeText, Text,
RenPrivateMessage, Object Descriptor, RenPrivateSourceFolder,
FileGroupsDescriptor, FileContents (despite adding them with SetData
in the original order).

Does anyone know why DataObject.SetData changes the order? Also, does
anyone know why I'm having problems saving/restoring an Outlook email?

public void SetButtonImage(string imageFilename)
{
Trace.WriteLine("SetButtonImage");
try
{
// save/restore data (in all of its formats) on the clipboard; this
is important so that we don't muck
// with the user's intention (for example, of pasting something into
a new email they create)
IDataObject ido = Clipboard.GetDataObject();
string[] formats = ido.GetFormats();
DataObject savedData = new DataObject();
foreach (string format in formats)
{
Trace.WriteLine(format);
savedData.SetData(format, ido.GetData(format));
}
Clipboard.SetDataObject(Image.FromFile(imagePathname));
Trace.Assert(this.button != null);
this.button.PasteFace();
Clipboard.SetDataObject(savedData);
}
catch (Exception e)
{
Trace.WriteLine(e);
}
}
}

Thanks,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top