• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4257
  • Last Modified:

"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
1
kevin33809
Asked:
kevin33809
  • 2
  • 2
2 Solutions
 
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?
0
 
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
0
 
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
0
 
Antagony1960Commented:
Glad to be of service... :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now