4

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.

a programmer
  • 959
  • 4
  • 8

4 Answers4

3

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
  • 344,408
  • 85
  • 689
  • 683
  • 3
    Thank you for your suggestion, but that doesn't answer my question. – a programmer Jul 05 '09 at 08:16
  • 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 Sep 27 '09 at 22:10
  • 3
    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 Mar 17 '10 at 03:28
  • 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 Sep 10 '10 at 05:11
  • 1
    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 Sep 10 '10 at 05:13
  • The commonly accepted way to copy formatted rich text into a word editor is to use the clipboard. I have yet to find a solution that provides an alternative, and I've tried. – Magnum Jan 31 '13 at 00:31
  • Magnum, for a *user* surely. For applications or automation there are other and better ways. – Joey Jan 31 '13 at 07:03
  • ROFL - tell that to Microsoft MapPoint developers who chose the clipboard as the main interface. – SChalice Jan 06 '16 at 19:07
0

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
  • 2,453
  • 1
  • 26
  • 24
  • 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 Feb 05 '10 at 09:09
0

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
  • 920
  • 1
  • 11
  • 22
0

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();
Crispy
  • 5,557
  • 3
  • 30
  • 35
  • 1
    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 Jul 05 '09 at 08:15
  • 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 Mar 17 '10 at 03:29