locked
VBS SendKeys issue RRS feed

  • Question

  • I cannot seem to get SendKeys to paste into a vbs input box. It copies just fine and can paste into notepad, word and other programs, just not the place I need it to. I can use the keyboard itself and I can paste all day long in the box I am trying to get data entered, but this is supposed to be an automated task. Currently working on trying to set this up to open as html to input and display data but I need to learn alittle more about that first. Can sendkeys paste into vbs? Because it is looking like no. Any advise to get this script working is appreciated.

    on error resume next

    dim dbconnection, sqlrs, itemno,sqlrs2, ITEM, objData

    Const CONNECT_STRING = "provider=SQLOLEDB.1;Presist Security info=True;User ID=sa;Initial Catalog=DEMOGOLF;Data Source=SERVER;password=PASSWORD"

    Set WshShell = WScript.CreateObject("WScript.Shell")
    set objFSO = CreateObject("Scripting.FileSystemObject")
    Set dbconnection = createobject("ADODB.connection")
    Set sqlrs = createobject("ADODB.Recordset")
    Set sqlrs2 = createobject("ADODB.Recordset")

    dbconnection.open CONNECT_STRING

    WshShell.AppActivate "Touchscreen Ticket Entry"
    WshShell.SendKeys ("%a")
    WshShell.SendKeys ("^C")

    wsh.sleep 1000

    Itemno = InputBox("Enter Item Number:", "Item Number")
    WshShell.AppActivate "Item Number"
    wsh.sleep 2000
    WshShell.SendKeys ("^V")

    '-------------------------------------------------------------------------
    '-----------Different ways I found and tried-------------------
    '-------------------------------------------------------------------------
    SendKeys.Send("^{v}")

    'WshShell.SendKeys ("+{Ins}")

    'WshShell.SendKeys ("^{vk56}")

    'SendKeys.KeyDown(Keys.ControlKey)
    'SendKeys.PressKey(Keys.V)
    'SendKeys.KeyUp(Keys.ControlKey)

    'Itemno = WshShell.SendKeys ("^V")
    '-------------------------------------------------------------------------
    '-------------------------------------------------------------------------
    '-------------------------------------------------------------------------
    msgbox ("Item Description "&sqlrs2("DESCR")& vbCrLf & "Current Average Cost: $"& ROUND(sqlrs("AVG_COST"),4)& vbCrLf & "Current Margin: "&ROUND(sqlrs2("MARGIN"),4)&"%"&""),0,"Item Number: "&sqlrs("ITEM_NO")
    'WshShell.SendKeys "{ENTER}"




    sqlrs.open "SELECT * FROM [IM_INV] where ITEM_NO = '"&Itemno&"' AND LOC_ID='MAIN'", CONNECT_STRING
    sqlrs2.open "SELECT * FROM [VI_USR_MARGIN] where ITEM_NO = '"&Itemno&"'", CONNECT_STRING
    If sqlrs.EOF Then 
        Wscript.Echo "Invalid Item. Please Re-Enter."
    Else
    msgbox ("Item Description "&sqlrs2("DESCR")& vbCrLf & "Current Average Cost: $"& ROUND(sqlrs("AVG_COST"),4)& vbCrLf & "Current Margin: "&ROUND(sqlrs2("MARGIN"),4)&"%"&""),0,"Item Number: "&sqlrs("ITEM_NO")
    END IF

    sqlrs.close
    dbconnection.close

    Wednesday, July 15, 2015 4:28 PM

Answers

  • What would other recommend?

    Rewrite your project so that it does not rely on SendKeys. Any unexpected pop-up, e.g. from some update, will derail your script.
    • Proposed as answer by Mike Laughlin Wednesday, July 15, 2015 7:01 PM
    • Marked as answer by Bill_Stewart Friday, August 14, 2015 8:06 PM
    Wednesday, July 15, 2015 6:33 PM

All replies

  • SendKeys is notoriously unreliable, as you're seeing.

    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Wednesday, July 15, 2015 4:38 PM
  • In this example it would be impossible.


    \_(ツ)_/

    Wednesday, July 15, 2015 4:51 PM
  • I cannot seem to get SendKeys to paste into a vbs input box.

    Would you ever write an application that requires users to type some input blindly, without ever looking at the screen to check where exactly they are? This is precisely what SendKeys does. It would work fine on a single-tasking machine but will fail regularly on a multi-tasking machine because you can never be sure which is the active window at any point in time. Generating Alt-Tab keystrokes by trial and error to switch to another window will work some of the time, then fail again.
    Wednesday, July 15, 2015 6:13 PM
  • Well it has done very well for many other projects that integrated many more commands.

    This project is the only one not working correctly due to inability to paste to input box.

    Just need to get it to paste into the input box. Pastes fine into everything else.

    Or assign what the WshShell.SendKeys ("^C") has copied to Itemno.

    What would other recommend?


    Wednesday, July 15, 2015 6:16 PM
  • What would other recommend?

    Rewrite your project so that it does not rely on SendKeys. Any unexpected pop-up, e.g. from some update, will derail your script.
    • Proposed as answer by Mike Laughlin Wednesday, July 15, 2015 7:01 PM
    • Marked as answer by Bill_Stewart Friday, August 14, 2015 8:06 PM
    Wednesday, July 15, 2015 6:33 PM
  • Do not use inputbox.  If you want the box to timeout then use a "popup" which has a timer.


    \_(ツ)_/

    Wednesday, July 15, 2015 7:37 PM