Getting "Cannot open clipboard" runtime error...

Hi experts, I have a program that moves some data around between TRichEdit's in order to produce and output a report, and I use the CopyToClipboard and PasteFromClipboard methods. Sometimes (not always) I encounted "Cannot open clipboard" runtime errors which shut down my program. Here's an example of how my code:

  
  Procedure CopyStringsToClip;
  var
    RE: TRichEdit;
  begin
     //create the temp RichEdit again and add the strings
    RE:= TRichEdit.Create(nil);
    RE.Visible:= False;
    RE.Parent:= self;
    RE.text := reportstr;
    RE.SelectAll;
    Clipboard.Clear;
    RE.CopyToClipboard;
    RE.Free;
  end;  // CopyStringsToClip

CopyStringsToClip;

RichRpt.Lines.BeginUpdate;
    RichRpt.SelStart:= 0;
    RichRpt.PasteFromClipboard;    
    Clipboard.Clear;
    sRichEdit1.SelectAll;  
    sRichEdit1.CopyToClipboard;
    RichRpt.SelStart:= Length(RichRpt.Text);
    RichRpt.PasteFromClipboard;
  RichRpt.Lines.EndUpdate;

Open in new window



 I'm now seeing that there are TClipboard methods Open and Close. Would using these solve my problem? If so, can I just place one TClipboard.Open at the very start just before I do all my clipboard shenanigans, and a TClipboard.Close right after I finish... or is there more to it than that?

Thanks
   Shawn

P.S: I'm using Delphi 7
shawn857Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
It tells you that the clipboard is just now not available. See Can't open Clipboard (Error 521).

You need to wait, till it's released from the locking application. E.g. a Sleep(1000).

See also this thread: "Run-time error 521 - Cannot open clipboard".

btw, use appropriate exception handling. And why still using D7? This means that you cannot support Win7 and above..
shawn857Author Commented:
thanks Ste5an I will try that... so should I surround my clipboard operations with Clipboard.Open and Clipboard.Close?

Thanks
   Shawn

P.S: My program written in D7 works fine in Windows 7 and above. I'm not sure what you mean.
ste5anSenior DeveloperCommented:
Maybe, but it normally means that another application has control over the clipboard. And you need to wait for this application to release it.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

shawn857Author Commented:
Ste5an - does calling Clipboard. Open attempt to "lock" the clipboard for my application? So if it's successful, I can use it with confidence until I issue a Clipboard.Close?

Thanks
   Shawn
ste5anSenior DeveloperCommented:
The normal clipboard actions do not require that you open and close it. The problem is: I don't have a Delphi right at hands. So the question is how is RE.CopyToClipboard implemented?
shawn857Author Commented:
I'm not sure what you mean. To implement RE.CopyToClipboard, it is simply how I showed in my sample code from my original question:

    RE.text := reportstr;
    RE.SelectAll;
    Clipboard.Clear;
    RE.CopyToClipboard;


Move the text to the RichEdit, select all of it, clear the cliboard, then issue the CopyToClipboard command.

Thanks
   Shawn
shawn857Author Commented:
Are you still there ste5an... ?

Cheers
   Shawn
ste5anSenior DeveloperCommented:
hi. sorry. I'm meant to implementation of TRichEdit.CopyToClipboard().
shawn857Author Commented:
Yes, just like I wrote two posts ago:

    RE.text := reportstr;
    RE.SelectAll;
    Clipboard.Clear;
    RE.CopyToClipboard;


Move the text to the RichEdit, select all of it, clear the cliboard, then issue the CopyToClipboard command.

I don't follow you.....

Thanks
    Shawn
ste5anSenior DeveloperCommented:
The implementation of TRichEdit.CopyToClipboard() is: SendMessage(Handle, WM_COPY, 0, 0);
The implmenentation of Clipboard.Clear() uses Open() and Close() internally with a finally clause.
So this code should be safe.

Possible issues: The clipboard is locked by a different thread or even a different process, thus another program.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shawn857Author Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.