VBA:SendKeys“^c”似乎正在复制到不同的剪贴板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24274840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:30:02  来源:igfitidea点击:

VBA: SendKeys "^c" seems to be copying to a different clipboard

vbaexcel-vbaexcel

提问by user3750428

I have code that copies a code from excel and pastes it into and application. If the code succeeds the cursor in the application automatically highlights text 0. I then have the keyboard do "ctrl-c", which copies it correctly in the fact that I can paste 0anywhere I choose by manually using my keyboard. But within the macro, using the paste function or directly accessing the clipboard doesn't give back this value, but other values copied in the macro at a prior line. Surprisingly enough, if I run my macro with a stop (big red dot) in the middle, after the "ctrl-c" command, it works fine, but then I have to run it in two steps.

我有从excel复制代码并将其粘贴到应用程序中的代码。如果代码成功,应用程序中的光标会自动突出显示文本0。然后我让键盘执行“ctrl-c”,它可以正确复制它,因为我可以0通过手动使用键盘将其粘贴到我选择的任何位置。但是在宏中,使用粘贴功能或直接访问剪贴板不会返回此值,而是返回宏中前一行复制的其他值。令人惊讶的是,如果我在中间有一个停止(大红点)运行我的宏,在“ctrl-c”命令之后,它工作正常,但我必须分两步运行它。

Here's the code:

这是代码:

Public Sub CopyUPCtoRMS()
Dim UPC As String
Dim SomeInRMS As Boolean
Dim i As Integer
Dim sht As Worksheet

Set sht = Worksheets(1)
i = 2

'While Not IsEmpty(Cells(i, 5))
    UPC = sht.Cells(i, 5).Copy
    AppActivate "Retek - prd"
    SendKeys "%r" & "{tab}{tab}{tab}", True 'reset the form
    SendKeys "^v" & "{ENTER}", True 'paste UPC into retek
    SendKeys "^c", True 'Copies '0' to a global clipboard.
    '**I put a stop here and run the code in 2 portions as "Microsoft Visual Basic" lets you, and it works, but in two parts... 
    sht.Range("F10").Value = ClipBoard_GetText '<--- Pastes the UPC, not '0'
'Wend
End Sub

I've tried replacing the SendKeys with a function that does something similar to the same results (I can post that code if needed), to no avail. Anyway, that's all I can think of for now, it took several hours to figure out what was even wrong...

我试过用一个函数替换 SendKeys,该函数执行类似于相同结果的操作(如果需要,我可以发布该代码),但无济于事。无论如何,这就是我现在所能想到的,花了几个小时才弄清楚到底是哪里出了问题......

采纳答案by VBlades

I've written an automation program like it seems you've done and have had similar issues. I think your last SendKeys call and the Clipboard_GetText call are happening in a timing where the copy function is not having time to complete before the next call executes. How I solved this in my application is grabbing the Sleep function from kernel32.dll (there is an article about it here: http://www.exceltrick.com/formulas_macros/vba-wait-and-sleep-functions/- have to declare it differently for 64 bit systems) by placing this declaration in a place your code can see it (same module should be fine - all the way at the top prior to any functions or subs):

我已经编写了一个自动化程序,就像您已经完成并且遇到了类似的问题一样。我认为您的最后一次 SendKeys 调用和 Clipboard_GetText 调用发生在复制函数在下一次调用执行之前没有时间完成的时间。我如何在我的应用程序中解决这个问题是从 kernel32.dll 中获取 Sleep 函数(这里有一篇关于它的文章:http: //www.exceltrick.com/formulas_macros/vba-wait-and-sleep-functions/- 必须对于 64 位系统,以不同的方式声明它)通过将此声明放在您的代码可以看到它的地方(相同的模块应该没问题 - 一直在任何函数或子程序之前的顶部):

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

And then after each SendKeys call, you can force your code to pause a bit; since time is in milliseconds, 1000 is one second. So, for example, you can call

然后在每次 SendKeys 调用之后,你可以强制你的代码暂停一下;由于时间以毫秒为单位,因此 1000 是一秒。因此,例如,您可以调用

Sleep 500

and there will be a half second pause between the next line of code being run. I think this may work for you, because when you put a breakpoint (the red dot), have the code stop, and run it again manually, it works, apparently. So I think it may be a timing issue that Sleep can help you out with. If that's the issue, play with the sleep time to keep the operation time to a minimum. Good luck.

并且在运行的下一行代码之间会有半秒的停顿。我认为这可能对你有用,因为当你放置一个断点(红点),让代码停止,然后手动再次运行它,它显然有效。所以我认为这可能是睡眠可以帮助你解决的时间问题。如果这是问题,请调整睡眠时间以将操作时间保持在最低限度。祝你好运。