Welcome to NavWin!
  

Get Clipboard Text In VB6

Have you ever got the error

‘Can’t open clipboard’

When you try to get the text from the clipboard, after simulating a  “select all and copy” on a document (SendKeys “^A”, SendKeys “^C”) then you need to allow time for the text to be actually placed on the clipboard. The best way to do this is to use a loop and a sleep function

 

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

 

Private Function GetClipboardText() As String

    On Error GoTo ErrorHandler

   

    Dim start As Date

    start = Now

   

TryAgain:

    DoEvents

    Sleep 300

    DoEvents

    Dim text As String

    text = Clipboard.GetText

    GetClipboardText = text

    Exit Function

ErrorHandler:

    Dim errNum As Long

    Dim errDescription As String

    errNum = Err.Number

    errDescription = Err.Description

   

    If DateDiff("s", Now, start) > 5 Then

        ' Allow time for the clipboard data to be sent to the clipboard

        ' Otherwise you get the error 'Can't open clipboard

        Err.Raise errNum, " GetClipboardText", errDescription

    Else

        Resume TryAgain

    End If

End Function