How to copy-paste from DOS application to Excel using SendKeys?

风格不统一 提交于 2021-01-27 19:44:53

问题


I'm trying to simulate this action:

  • activate another application,
  • send keystroke ctrl+c,
  • go back to Excel,
  • send key stroke ctrl+v

and have that value in a cell.

It's a DOS window style application, so keystroke is the only way to manage it.

I managed to activate and to input keystrokes such as ENTER into that DOS style application, but when I try ctrl+C it is not seen to do anything.

I tried simulating it in Excel VBA with:

Range("E7").Select
SendKeys "^c"
Range("G7").Select
SendKeys "^v"

The E7 value is not copied, but G7 (paste destination) is highlighted as if it was selected for copying.

Note: I am not trying to copy things from Excel to Excel, but to execute keystrokes using Excel.


回答1:


I ran into the same issue today.

I solved it by waiting a bit after sending CTRL-C to the application. It seems nothing is copied if you immediately execute another script line.

SendKeys "^c"
WScript.Sleep 100 ' wait a bit after CTRL+C (otherwise nothing is copied)

In fact, the same issue seems to happen when switching to another app and sending CTRL+V. Again, I solved it by waiting a bit:

AppActivate "Microsoft Excel"
WScript.Sleep 100 ' wait a bit after making window active
SendKeys "^v"



回答2:


The problem is that copying and pasting takes some time and VBA is not waiting for it.

First you need to specify second argument of send keys method which is "Wait". Set it to true. Thus VBA execution is waiting for until sending keys is completed.

Secondly you need to wait until process of copying data to clipboard is completed. "Wait" in sendkeys is not doing it because it's not about sendking keys by VBA but it's about Windows working with clipboard. To do it please use my function IsCopyingCompleted.

Here's how final can look like:

SendKeys "^a", True 'Select all
SendKeys "^c", True 'Copy
Do
    DoEvents
Loop Until Me.IsCopyingCompleted()
YourSheet.Paste

Function IsCopyingCompleted() As Boolean
'Check if copying data to clipboard is completed

    Dim tempString As String
    Dim myData As DataObject

    'Try to put data from clipboard to string to check if operations on clipboard are completed
    On Error Resume Next
    Set myData = New DataObject
    myData.GetFromClipboard
    tempString = myData.GetText(1)
    If Err.Number = 0 Then
        IsCopyingCompleted = True
    Else
        IsCopyingCompleted = False
    End If
    On Error GoTo 0

End Function



回答3:


I had this same problem - I developed code and it worked on my computer - SendKeys "^v"...but it did not work on another user's computer! I was trying EVERYTHING...delays, appreciate, accessig access from Excel, having the user check various settings, selection.paste...no good. Finally I noticed different syntaxes...we were using the same office version, I'm not sure about Windows. But I entered 6 different syntaxes and had it skip over if it failed....SendKeys with a capital v, enclosed in squigley brackets, each component in squigleys separated by a plus sign, etc. IT WORKED! 2 of the commands put the ^V in the body of outlook...I remove those 2 and I'm golden.



来源:https://stackoverflow.com/questions/15282825/how-to-copy-paste-from-dos-application-to-excel-using-sendkeys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!