Question

I have problem with reading data from clipboard at fast rate. I have one program that sends data 10 times per sec to clipboard, and now in VBA I want to recive that data.

Code im using:

Dim clipboard As MSForms.DataObject
Dim strContents As String
Set clipboard = New MSForms.DataObject

Do While True

clipboard.GetFromClipboard
strContents = clipboard.GetText
clipboard.Clear

Myfunc(strContents)

ThisApplication.StatusBarText = strContents  'debug 
If Left(strContents, 1) = 9 Then Exit Sub 'end condition
Set clipboad = Nothing

sleep 100
Loop

End Sub

There is problem: this code is working for about 2 sec and then I get error: DataObject:GetText 8007000E - Ran out of memory. The clipboard data is non stop this same. Im clearing objects every loop. Where is problem? Im on 64 bit windows 7, 64 bit VBA application, 8 gigs of ram.

Était-ce utile?

La solution

As Patrick pointed out, you should move the Set clipboard = Nothing below the loop. Also, there is a typo, as it is currently written as set clipboad = Nothing.

In addition, insert DoEvents after you clear your clipboard. This will make sure that the operating system has time to clear it before you read it again.

Your final code should look somehow like this:

Dim clipboard As MSForms.DataObject
Dim strContents As String
Set clipboard = New MSForms.DataObject

Do While True
    clipboard.GetFromClipboard
    strContents = clipboard.GetText
    clipboard.Clear
    DoEvents

    Myfunc(strContents)

    ThisApplication.StatusBarText = strContents  'debug 
    If Left(strContents, 1) = 9 Then Exit Sub 'end condition

    sleep 100
Loop

Set clipboard = Nothing
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top