Search code examples
excelvbasendkeys

vba function "sendkeys" does not send anything


It seems that "sendkeys" is only good for turning off your numbers lock. It does not appear to do anything else. I've tried many different ways to simply write random characters to notepad with no success. My only hunch is this ability has been disabled by a recent Windows update as it used to work fine until last week. Below is some example code. Does anyone know how to get it to send keys again?

Public Sub WriteToNotePad()

    Dim vReturn As Variant
    vReturn = Shell("NotePad.exe", vbNormalFocus) 'open notepad

    AppActivate vReturn 'ensure notepad is active
    Application.Wait (Now() + TimeValue("00:00:03")) 'wait a few seconds
 
    SendKeys "You should see this in notepad", True
   
End Sub

Solution

  • I used a computer that didn't get the Office 365 update & the code worked fine. So I'm certain that's the problem. To fix it, I used John Coleman's suggestion to write a VBS file instead & then call that script from the VBA code.

    From the above example, change this:

    SendKeys "You should see this in notepad", True
    

    To this:

    Call Shell("wscript C:\writeToNotepad.vbs", vbNormalFocus)
    

    And the VBS file would be saved in that location with that name & with this content:

    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.SendKeys "You should see this in notepad"