"Run-time error 521 - Cannot open clipboard"

I'm working on a program that allows me to copy a few selected records of a listbox to the Clipboard, but when I try to copy the whole list of 1017 records.  I get the following error --"Run-time error 521 - Cannot open clipboard".  See code below where the alleged error occurs.

Some of the things that I've tried to help the Clipboard along are -- 1. using a Sleep function to placate the process for a few seconds so Clipboard could populate.  I've also tried the, 2. DoEvents to allow processing while Clipboard populates.  Neither works.  The error seems to occurs at the following line -- Clipboard.SetText.

Any help would be appreciated.  Thank you.


    'Export from Whole Grid in TXT TAB Format to Clipboard
    If optRange(0).Value = True And optFormat(0).Value = True And optTarget(0).Value = True Then
        If chkHdrs.Value = 1 Then
            For j = 0 To tdbGrid.Columns.Count - 1
                cs = IIf(tdbGrid.Columns(j).Caption & "" = "", "", tdbGrid.Columns(j).Caption)
                strTemp = strTemp & cs & vbTab
            Next
            strTemp = strTemp & vbCrLf
        End If
        If xFilter.Count(2) > 0 Then
            For n = 0 To xFilter.UpperBound(1)
                For j = 0 To xFilter.UpperBound(2)
                    cs = IIf(xFilter(n, j) & "" = "", "", xFilter(n, j))
 
                    strTemp = strTemp & cs & vbTab
                Next
               strTemp = strTemp & vbCrLf                    <<<  Accumulated records
               Clipboard.Clear                                         <<<< Clear clipboard
               Clipboard.SetText strTemp, vbCFText      <<< populate clipboard.  Error occurs here.
           Next
        Else
            For n = 0 To xda.UpperBound(1)
                For j = 0 To xda.UpperBound(2)
                    cs = IIf(xda(n, j) & "" = "", "", xda(n, j))
                    cs = Replace(cs, vbLf, vbLf & String(j, vbTab))
                    strTemp = strTemp & cs & vbTab
                Next
               strTemp = strTemp & vbCrLf                    <<<  Accumulated records
               Clipboard.Clear                                         <<<< Clear clipboard
               Clipboard.SetText strTemp, vbCFText      <<< populate clipboard.  Error occurs here.
             Next
        End If
        Sleep 10000    <<< Wait for 10 seconds.
        MsgBox "Data copied successfully to clipboard", vbExclamation, "Exporting Data..."
    End If

Open in new window

Runtime-error-521.jpg
kevin33809Asked:
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.

Antagony1960Commented:
I remember having this problem in n app many years ago and it turned out that all I needed to do was add a pause--DoEvents isn't enough so use "Sleep 1000"--between the Clear and the SetText.

One thing I've noticed about your code however, is that you're clearing and setting the clipboard on every iteration of the for-next loops starting on lines 11 & 22. Shouldn't your clipboard code be outside of those loops, after the Next statements?
kevin33809Author Commented:
Thanks Antagony for responding.  I've tried what you suggested, but still got the message after what felt like a minute.  Long time.  See code below, it shows the placement of the Sleep function.  Is this what you meant?  I'm open to more suggestions.  And thanks again.


    'Export from Whole Grid in TXT TAB Format to Clipboard
    If optRange(0).Value = True And optFormat(0).Value = True And optTarget(0).Value = True Then
        If chkHdrs.Value = 1 Then
            For j = 0 To tdbGrid.Columns.Count - 1
                cs = IIf(tdbGrid.Columns(j).Caption & "" = "", "", tdbGrid.Columns(j).Caption)
                strTemp = strTemp & cs & vbTab
            Next
            strTemp = strTemp & vbCrLf
        End If
        If xFilter.Count(2) > 0 Then
            For n = 0 To xFilter.UpperBound(1)
                For j = 0 To xFilter.UpperBound(2)
                    cs = IIf(xFilter(n, j) & "" = "", "", xFilter(n, j))

                    strTemp = strTemp & cs & vbTab
                Next
                strTemp = strTemp & vbCrLf
            Next
            Clipboard.Clear
            Sleep 1000
            Clipboard.SetText strTemp, vbCFText
        Else
            For n = 0 To xda.UpperBound(1)
                For j = 0 To xda.UpperBound(2)
                    cs = IIf(xda(n, j) & "" = "", "", xda(n, j))
                    cs = Replace(cs, vbLf, vbLf & String(j, vbTab))
                    strTemp = strTemp & cs & vbTab
                Next
                strTemp = strTemp & vbCrLf
                Clipboard.Clear
                Sleep 1000
                Clipboard.SetText strTemp, vbCFText
            Next
        End If
        MsgBox "Data copied successfully to clipboard", vbExclamation, "Exporting Data..."
    End If
kevin33809Author Commented:
Antagony
I was able to get it to work.  I went back into the code and move the Clipboard code and Sleep outside the second NEXT then it work.  See code below.  Again thanks.  You get 500 points.

     For n = 0 To xda.UpperBound(1)
                For j = 0 To xda.UpperBound(2)
                    cs = IIf(xda(n, j) & "" = "", "", xda(n, j))
                    cs = Replace(cs, vbLf, vbLf & String(j, vbTab))
                    strTemp = strTemp & cs & vbTab
                Next
                strTemp = strTemp & vbCrLf
                Clipboard.Clear
                Sleep 1000
                Clipboard.SetText strTemp, vbCFText
            Next

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
Antagony1960Commented:
Glad to be of service... :-)
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
Programming Languages-Other

From novice to tech pro — start learning today.