在 VBA 中执行下一个命令之前强制方法 (GetFromClipboard) 完成 [英] Force method (GetFromClipboard) to finish before executing next command in VBA

查看:114
本文介绍了在 VBA 中执行下一个命令之前强制方法 (GetFromClipboard) 完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 PutInClipboard 方法将变量复制到剪贴板.由于 Win10 中的一个已知错误,我需要验证剪贴板的内容是否确实是它所假设的内容.

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.

不幸的是它没有按预期工作,我需要使用 wait-Methode强制"PutInClipboard-方法,否则比较返回 true,即使值不应相同:

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   

我想知道是否有不同的方法来强制完全执行 PutInClipboard 方法然后使用 wait()?!目标是获得正确的结果,但尽可能减少等待时间".

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.

推荐答案

你也可以将你的宏分成 2 部分(例如:Macro1Macro2)并在您将使用的第一个宏的结尾

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"

在等待 1 秒后执行代码的第二部分.

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

根据我的经验,这比使用 Wait 甚至 DoEvents 更可靠,因为这将确保 VBA 过程完全结束一秒钟.据我了解,这让 Excel 和操作系统优先完成需要完成的操作,即:将字符串内容放入剪贴板.

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.

这篇关于在 VBA 中执行下一个命令之前强制方法 (GetFromClipboard) 完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆