tags:

views:

229

answers:

4

Hi all,

Is there a way to preserve the contents of the clipboard? I tried the following code but it doesn't work.

Dim iData As IDataObject = Clipboard.GetDataObject()
...(use clipboard)
Clipboard.SetDataObject(iData)

Thank you.

+3  A: 

The easiest way to preserve the contents of the clipboard is to leave the clipboard alone. The clipboard is meant as a temporary storage area for the user, not for applications, so likely what you are trying to do has better solutions than to clobber the clipboard.

Joey
Thank you for your suggestion, but that doesn't answer my question.
a programmer
Well, it'd be nice if you would include the reason why you want to do that in your question. Then it's easier to devise a solution which does what you want. I still consider clobbering the clipboard temporarily a very stupid idea.
Joey
There are no valid reasons for doing this. “Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.” — Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992
Chris Thornton
I came across this question because I wanted to do just the same. Regardless of the "WHY" the problem with the .Net class is that you can't tell what the underlying of the format of the data is so you can't put it back with the right format. In short it lacks a GetForamt() method. Polling ContainsData() for each standard format is not really good enough.
Swanny
As for the "WHY". In my case I wanted to try and squeeze a bit of functionality out of the WebBrowser control without having to resort to low level stuff. I can use the clipboard to paste in the chunk I want to insert, but not without overwriting what's already in the clipboard.
Swanny
A: 

You can use the OpenClipboard and CloseClipboard. According to MSDN opening the clipboard will keep other applications from changing the data.

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool CloseClipboard();
Chris Persichetti
Thank you for your answer, but what I want is to be able to store the clipboard contents, use the clipboard for a copy/paste, and then restore the original contents.
a programmer
Holding the clipboard open will also CRASH other applications when they try to open the clipboad, can't, and don't handle it properly.
Chris Thornton
A: 

In what way did your code above not work? When I try the equivalent code in C# I get a "CloseClipboard Failed (Exception from HRESULT: 0x800401D4 (CLIPBRD_E_CANT_CLOSE))" exception on calling Clipboard.SetDataObject(iData).

However, the following workaround does the job for me:

// save
Dictionary<String, Object> d = new Dictionary<String, Object>();
IDataObject ido = Clipboard.GetDataObject();
foreach (String s in ido.GetFormats(false))
    d.Add(s, ido.GetData(s));

// ...

// restore
var da = new DataObject();
foreach (String s in d.Keys)
    da.SetData(s, d[s]);
Clipboard.SetDataObject(da);
Edward D'Souza
Thak you for answer, I tried it and it works for plain text. Unfortunately, it throws an error when the text in the clipboard is from MS Word.
a programmer
A: 

I agree that the context is important. In my case, I wanted to paste a formatted, dynamically filled-in cover page document onto the front of some dynamically generated text (all in MS Word). Here's the solution I found (using VSTO and C#):

                object start = 0;
                Word.Range startRng = a_TreatedDocument.Range(ref start, ref start);
                startRng.FormattedText = a_CoverPageDocument.Content.FormattedText;

Note, this works with tables and formatted text.

Daniel