Link to home
Start Free TrialLog in
Avatar of kevin33809
kevin33809Flag for United States of America

asked on

"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
SOLUTION
Avatar of Antagony1960
Antagony1960

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kevin33809

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Antagony1960
Antagony1960

Glad to be of service... :-)