0

I want to copy a variable to the clipboard using the PutInClipboard-methode. Due to a known bug in Win10 I need to verify if the content of the clipboard is actually what it's suppose to be.

Unfortunalty it does not work as expected and I need to "enforce" the PutInClipboard-methode by using the wait-Methode, otherwise the comparison returns true, even though the values should not be the same:

'Put the content of a variable into the clipbaord
Dim strDesiredClipboardContent as String
Dim dataObject1 As DataObject
Set dataObject1 = New DataObject
dataObject1.SetText strDesiredClipboardContent
dataObject1.PutInClipboard

'Enforce Execution (otherwise the comparison in the end does not work)
Application.Wait Now + #12:00:01 AM#

'Get whatever is in the clipboard
Dim strActuallClipboardContent
Dim dataObject2 As MSForms.DataObject
Set dataObject2 = New MSForms.DataObject
dataObject2.GetFromClipboard    
strActuallClipboardContent = dataObject2.GetText

'Compare
If strDesiredClipboardContent <> strActuallClipboardContent Then
    MsgBox "Error"
    End If

End Sub   

I wonder if there is a different methode to enforce the complete execution of the PutInClipboard-methode then using wait()?! The goal would be to get the correct results but to ad as little "wait-time" as possible.

Albin
  • 1,000
  • 1
  • 11
  • 33

1 Answers1

2

You could also seperate your macro into 2 parts (e.g.: Macro1 and Macro2) and at the end of the first macro you would use

Application.Ontime Now + TimeSerial(0, 0, 1), "Macro2"

to execute the second part of your code after waiting 1 second.

In my experience, this is more reliable than using Wait or even DoEvents since this will make sure the VBA process completely ends for a brief second. From what I understand, this leaves priority to Excel and the OS to complete the operations that needs to be done i.e.: put string content in the clipboard.

DecimalTurn
  • 3,243
  • 3
  • 16
  • 36
  • Using Ontime, might be worth a try, although 2 seconds for a copy&paste action is a little too much. Eventually I'm looking for s.th. shorter. – Albin Sep 10 '19 at 07:21
  • 1
    Just try it without +Timeserial(...), Application.ontime makes Macro2 run After Macro1 is ended anyway, (@DC +1) – EvR Sep 10 '19 at 08:14
  • @Albin, if you use Ontime, you don't need to use the Wait method, so you could just have a 1 sec waiting time or a 0 sec waiting time as suggested by EvR. – DecimalTurn Sep 10 '19 at 17:26
  • @DecimalTurn I'll give it a try with EvR's suggestion and let you know... anyway +1 :) – Albin Sep 10 '19 at 17:36
  • @DecimalTurn seems to work at least during the first tests. The problem is, that I'm not calling a macro but a procedure with parameters. OnTime only allows me to run macros. To get around that problem I save the parameter in a global variable and run a macro which then runs the actual procedure with the input parameter which it gets from the global variable. Is there another solution to this? I try tried to run a "pseudo macro" before running the procedure but that does not seem to make any difference?! – Albin Sep 16 '19 at 17:11
  • @Albin - Your method of using global variables is probably the way to go. I can't think of any other way personally. – DecimalTurn Sep 16 '19 at 21:49
  • @DecimalTurn yeah, not a very elegant way, but thanks. For the suggestion. Do you have any idea why the "pseudo macro" I inserted in between doesn't work? – Albin Sep 17 '19 at 07:49
  • @Albin - I'm not sure I understand what you mean by a pseudo macro – DecimalTurn Sep 17 '19 at 18:09
  • @DecimalTurn basically a macro that doesn't do anything productive (e.g. just debug.print "something"). I just use it "in-between" to get the Ontime executed. This way I don't have to go through the global variable. Let me know if you still don't get it, then I put in a new question with code. – Albin Sep 17 '19 at 20:41
  • @Albin - I think it would be easier if I could see the code. – DecimalTurn Sep 17 '19 at 22:49