How to know that the data on the clipboard is ready to be used?

Viewed 89

My code copies a table, processes the copied data, copies another table, processes the copied data and so on ...

The problem is that sometimes some tables have a lot of data that take a long time to be copied, causing an error in the try/catch block.

Is there a way to start processing the copied data only after the data is ready on the clipboard?

   Private Sub Copy
      Clipboard.Clear()
            Try
                Win32.SetCursorPos(200,200)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                SendKeys.SendWait("^C")
            Catch ex As Exception
                Msgbox (ex.message)
            End Try


            'PROCESS THE COPIED DATA
        Try
            Dim clip() As String
            Dim col() As String
            clip = Clipboard.GetText().Split(Environment.NewLine)
            col = clip(i).Split(vbTab)
            'I test the data to see if it interests me
        Catch ex As Exception

        End Try


     Clipboard.Clear()
            Try
                Win32.SetCursorPos(600,200)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                SendKeys.SendWait("^C")
            Catch ex As Exception
                Msgbox (ex.message)
            End Try



            'PROCESS THE COPIED DATA


     Clipboard.Clear()
            Try
                Win32.SetCursorPos(400,200)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                SendKeys.SendWait("^C")
            Catch ex As Exception
                Msgbox (ex.message)
            End Try


            'PROCESS THE COPIED DATA


    End Sub
1 Answers

Here is a loop as per @jmcilhinney 's comment. No GoTo. Set the number of tries in the If ErrorCount > 3 Then. You were repeating your code. Usually not a good idea for maintenance. The only thing that changed was the left and top, so I passed those as parameters. The display of the message box will provide the pause. Your ProcessCopiedData should probably be a separate sub called from the Copy sub.

Private Sub Copy(left As Integer, top As Integer)
    Dim ErrorCount As Integer
    Do
        Clipboard.Clear()
        Try
            Win32.SetCursorPos(left, top)
            Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
            Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
            SendKeys.SendWait("^C")
            ErrorCount = 0
        Catch ex As Exception
            ErrorCount += 1
            MsgBox(ex.Message)
        End Try
        If ErrorCount > 3 Then
            MessageBox.Show("Exceeded # of Trys.")
            Exit Sub
        End If
    Loop Until ErrorCount < 1
    'Process Copied Data
End Sub

Usage...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Copy(200, 200)
    Copy(600, 200)
    Copy(400, 200)
End Sub
Related