[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] [已解决]VBS怎样根据输入的数值运行不同的按键动作?

本帖最后由 qiaoliangih 于 2011-5-11 00:29 编辑

本人在医院工作,经常输入一些重复的药名与化验
所以做了一个vbs,全部用WshShell.SendKeys写的。但功能不完善
有些病号用药多,有些少,化验也是如此,我不得的不每次运行完vbs后修改录入记录。
我想让大家帮忙写个高级一点的。
运行vbs后先会询问这个药需要几个,按照输入的值去输入药量。
然后第二次询问另外一种药,如果是0就不输入,如果是1就输入1,是2就是输入2。
第三次询问有无特殊化验(默认是输出3种化验),如是0,则不输出特殊化验,如果是1,就输出特殊化验。
请大家帮帮忙,谢谢

该脚本我已经使用了大半年,今日想起,心存感激,特来再次感谢

TOP

这个好用,留个记号!!!!

TOP

17# fastslz
18# fastslz
谢谢,谢谢fastslz 大大给出的完美解决办法,实在是太感谢了!!!!!!

TOP

本帖最后由 fastslz 于 2011-5-10 14:59 编辑

再给你一个方法
  1. Dim tangshui,gansu,touxi,wshshell,aStrs,bStrs,cStrs,dStrs
  2. Set WshShell = CreateObject ("Wscript.Shell")
  3. input=InputBox("糖水瓶数")
  4. aSelect(input)
  5. input=InputBox("肝素支数")
  6. bSelect(input)
  7. input=InputBox("透析器次数")
  8. cSelect(input)
  9. If Not  aStrs = "" Then
  10.    KeysNames = Split(aStrs, ",")
  11.    WshShell.AppActivate "程序窗体名称"           '用于激活你的程序窗体,不至于在其他窗体上乱按
  12.    For i = 0 to UBound(KeysNames)
  13.        Wscript.sleep 20
  14.        Wshshell.SendKeys KeysNames(i)
  15.    Next
  16. End If
  17. If Not  bStrs = "" Then
  18.    KeysNames = Split(bStrs, ",")
  19.    WshShell.AppActivate "程序窗体名称"           '用于激活你的程序窗体,不至于在其他窗体上乱按
  20.    For i = 0 to UBound(KeysNames)
  21.        Wscript.sleep 20
  22.        Wshshell.SendKeys KeysNames(i)
  23.    Next
  24. End If
  25. dStrs = "xytx,{ENTER},{ENTER},{ENTER},{ENTER},{ENTER},xtjc,{ENTER},{ENTER},{ENTER},{ENTER},{ENTER},ycxg,{ENTER},{ENTER},{ENTER},{ENTER}"
  26. If Not  dStrs = "" Then
  27.    KeysNames = Split(dStrs, ",")
  28.    WshShell.AppActivate "程序窗体名称"           '用于激活你的程序窗体,不至于在其他窗体上乱按
  29.    For i = 0 to UBound(KeysNames)
  30.        Wscript.sleep 20
  31.        Wshshell.SendKeys KeysNames(i)
  32.    Next
  33. End If
  34. If Not  cStrs = "" Then
  35.    KeysNames = Split(cStrs, ",")
  36.    WshShell.AppActivate "程序窗体名称"           '用于激活你的程序窗体,不至于在其他窗体上乱按
  37.    For i = 0 to UBound(KeysNames)
  38.        Wscript.sleep 20
  39.        Wshshell.SendKeys KeysNames(i)
  40.    Next
  41. End If
  42. Function aSelect(N)
  43.     Select Case N
  44.         Case 0
  45.         aStrs = ""
  46.              Exit Function
  47.         Case Else
  48.              aStrs = "lhn,{ENTER},{ENTER}," & N & ",{ENTER},{ENTER},{ENTER}"
  49.     End Select
  50. End Function
  51. Function bSelect(B)
  52.     Select Case B
  53.         Case 0
  54.         bStrs = ""
  55.              Exit Function
  56.         Case Else
  57.              bStrs = "gsz,{ENTER},{ENTER}," & B & ",{ENTER},{ENTER},{ENTER}"
  58.     End Select
  59. End Function
  60. Function cSelect(c)
  61.     Select Case c
  62.         Case 0
  63.         cStrs = ""
  64.              Exit Function
  65.         Case Else
  66.              cStrs = "txq,{ENTER},{ENTER},{ENTER},{ENTER}"
  67.     End Select
  68. End Function
复制代码

TOP

本帖最后由 fastslz 于 2011-5-10 15:01 编辑
  1. Dim tangshui,gansu,touxi,wshshell,aStrs,bStrs,cStrs
  2. Set WshShell = CreateObject ("Wscript.Shell")
  3. input=InputBox("糖水瓶数")
  4. aSelect(input)
  5. input=InputBox("肝素支数")
  6. bSelect(input)
  7. input=InputBox("透析器次数")
  8. cSelect(input)
  9. If Not  aStrs = "" Then
  10.    KeysNames = Split(aStrs, ",")
  11.    For i = 0 to UBound(KeysNames)
  12.        Wshshell.SendKeys KeysNames(i)
  13.    Next
  14. End If
  15. If Not  bStrs = "" Then
  16.    KeysNames = Split(bStrs, ",")
  17.    For i = 0 to UBound(KeysNames)
  18.        Wshshell.SendKeys KeysNames(i)
  19.    Next
  20. End If
  21. Wshshell.SendKeys "xytx"
  22. Wscript.sleep 20 '若想连续模拟按键稳定性,每个模拟按键延时20毫秒,延时多少取决于你的计算机性能
  23. Wshshell.SendKeys "{ENTER}"
  24. Wscript.sleep 20
  25. Wshshell.SendKeys "{ENTER}"
  26. Wscript.sleep 20
  27. Wshshell.SendKeys "{ENTER}"
  28. Wscript.sleep 20
  29. Wshshell.SendKeys "{ENTER}"
  30. Wscript.sleep 20
  31. Wshshell.SendKeys "{ENTER}"
  32. Wshshell.SendKeys "xtjc"
  33. Wscript.sleep 20
  34. Wshshell.SendKeys "{ENTER}"
  35. Wscript.sleep 20
  36. Wshshell.SendKeys "{ENTER}"
  37. Wscript.sleep 20
  38. Wshshell.SendKeys "{ENTER}"
  39. Wscript.sleep 20
  40. Wshshell.SendKeys "{ENTER}"
  41. Wscript.sleep 20
  42. Wshshell.SendKeys "{ENTER}"
  43. Wshshell.SendKeys "ycxg"
  44. Wscript.sleep 20
  45. Wshshell.SendKeys "{ENTER}"
  46. Wscript.sleep 20
  47. Wshshell.SendKeys "{ENTER}"
  48. Wscript.sleep 20
  49. Wshshell.SendKeys "{ENTER}"
  50. Wscript.sleep 20
  51. Wshshell.SendKeys "{ENTER}"
  52. If Not  cStrs = "" Then
  53.    KeysNames = Split(cStrs, ",")
  54.    For i = 0 to UBound(KeysNames)
  55.        Wshshell.SendKeys KeysNames(i)
  56.    Next
  57. End If
  58. Function aSelect(N)
  59.     Select Case N
  60.         Case 0
  61.         aStrs = ""
  62.              Exit Function
  63.         Case Else
  64.              aStrs = "lhn,{ENTER},{ENTER}," & N & ",{ENTER},{ENTER},{ENTER}"
  65.     End Select
  66. End Function
  67. Function bSelect(B)
  68.     Select Case B
  69.         Case 0
  70.         bStrs = ""
  71.              Exit Function
  72.         Case Else
  73.              bStrs = "gsz,{ENTER},{ENTER}," & B & ",{ENTER},{ENTER},{ENTER}"
  74.     End Select
  75. End Function
  76. Function cSelect(c)
  77.     Select Case c
  78.         Case 0
  79.         cStrs = ""
  80.              Exit Function
  81.         Case Else
  82.              cStrs = "txq,{ENTER},{ENTER},{ENTER},{ENTER}"
  83.     End Select
  84. End Function
复制代码
下面的代码更稳定,但必须安装word,例子为循环等待记事本窗体,发现窗体就模拟按键,仅供参考
  1. Dim tangshui,gansu,touxi,wshshell,aStrs,bStrs,cStrs
  2. Set WshShell = CreateObject ("Wscript.Shell")
  3. input=InputBox("糖水瓶数")
  4. aSelect(input)
  5. input=InputBox("肝素支数")
  6. bSelect(input)
  7. input=InputBox("透析器次数")
  8. cSelect(input)
  9. If Not  aStrs = "" Then
  10.    KeysNames = Split(aStrs, ",")
  11.    For i = 0 to UBound(KeysNames)
  12.        aWindow("记事本")
  13.        Wshshell.SendKeys KeysNames(i)
  14.    Next
  15. End If
  16. If Not  bStrs = "" Then
  17.    KeysNames = Split(bStrs, ",")
  18.    For i = 0 to UBound(KeysNames)
  19.        aWindow("记事本")
  20.        Wshshell.SendKeys KeysNames(i)
  21.    Next
  22. End If
  23. aWindow("记事本")
  24. Wshshell.SendKeys "xytx"
  25. Wscript.sleep 20 '若想连续模拟按键稳定性,每个模拟按键延时20毫秒,延时多少取决于你的计算机性能
  26. Wshshell.SendKeys "{ENTER}"
  27. Wscript.sleep 20
  28. Wshshell.SendKeys "{ENTER}"
  29. Wscript.sleep 20
  30. Wshshell.SendKeys "{ENTER}"
  31. Wscript.sleep 20
  32. Wshshell.SendKeys "{ENTER}"
  33. Wscript.sleep 20
  34. Wshshell.SendKeys "{ENTER}"
  35. aWindow("记事本")
  36. Wshshell.SendKeys "xtjc"
  37. Wscript.sleep 20
  38. Wshshell.SendKeys "{ENTER}"
  39. Wscript.sleep 20
  40. Wshshell.SendKeys "{ENTER}"
  41. Wscript.sleep 20
  42. Wshshell.SendKeys "{ENTER}"
  43. Wscript.sleep 20
  44. Wshshell.SendKeys "{ENTER}"
  45. Wscript.sleep 20
  46. Wshshell.SendKeys "{ENTER}"
  47. aWindow("记事本")
  48. Wshshell.SendKeys "ycxg"
  49. Wscript.sleep 20
  50. Wshshell.SendKeys "{ENTER}"
  51. Wscript.sleep 20
  52. Wshshell.SendKeys "{ENTER}"
  53. Wscript.sleep 20
  54. Wshshell.SendKeys "{ENTER}"
  55. Wscript.sleep 20
  56. Wshshell.SendKeys "{ENTER}"
  57. If Not  cStrs = "" Then
  58.    KeysNames = Split(cStrs, ",")
  59.    For i = 0 to UBound(KeysNames)
  60.        aWindow("记事本")
  61.        Wshshell.SendKeys KeysNames(i)
  62.    Next
  63. End If
  64. Function aSelect(N)
  65.     Select Case N
  66.         Case 0
  67.         aStrs = ""
  68.              Exit Function
  69.         Case Else
  70.              aStrs = "lhn,{ENTER},{ENTER}," & N & ",{ENTER},{ENTER},{ENTER}"
  71.     End Select
  72. End Function
  73. Function bSelect(B)
  74.     Select Case B
  75.         Case 0
  76.         bStrs = ""
  77.              Exit Function
  78.         Case Else
  79.              bStrs = "gsz,{ENTER},{ENTER}," & B & ",{ENTER},{ENTER},{ENTER}"
  80.     End Select
  81. End Function
  82. Function cSelect(c)
  83.     Select Case c
  84.         Case 0
  85.         cStrs = ""
  86.              Exit Function
  87.         Case Else
  88.              cStrs = "txq,{ENTER},{ENTER},{ENTER},{ENTER}"
  89.     End Select
  90. End Function
  91. Sub aWindow(WindowT)
  92. Set objWord = CreateObject("Word.Application")
  93. Set colTasks = objWord.Tasks
  94. Do
  95.     If colTasks.Exists(WindowT) Then
  96.        colTasks(WindowT).Activate
  97.        colTasks(WindowT).WindowState = 0
  98.        Exit Do
  99.     End If
  100.     Wscript.sleep 200
  101. Loop
  102. objWord.Quit
  103. End Sub
复制代码

TOP

现在我陷入困境了,彻底没有招了

TOP

8楼顺序问题,你自己解决吧
就代码不稳定问题我给你一些参考

vbs模拟键盘最不稳定有2大问题,当前窗体及延时,延时还取决于硬件性能

在指定的窗体发送模拟键
  1. '这个下面的例子激活“计算器”窗体发送1+1=模拟键
  2. Set WshShell = Wscript.CreateObject("Wscript.Shell")
  3. WshShell.AppActivate "计算器"
  4. WshShell.SendKeys "1{+}1~"
复制代码
上面的还有点欠缺不能在"计算器"最小化时不能将其激活
  1. '这个例子需要安装有office 2003以上的系统中运行,
  2. '如果有“计算器”窗体就激活该窗体,激活后将其最大化并发送1+1=模拟键
  3. Set WshShell = Wscript.CreateObject("Wscript.Shell")
  4. Set objWord = CreateObject("Word.Application")
  5. Set colTasks = objWord.Tasks
  6. If colTasks.Exists("计算器") Then
  7.     colTasks("计算器").Activate
  8.     colTasks("计算器").WindowState = 1
  9.     WshShell.SendKeys "1{+}1~"
  10. End If
  11. objWord.Quit
复制代码

TOP

13# batman
恩,是需要Wshshell.SendKeys的,因为是要输入另外一个软件的。目前只有vbs模拟键盘比较有效

TOP

12# qiaoliangih
非要用sendkey,直接输入不行?
  1. '输入exit退出循环
  2. Dim vbstr, wssh, fso
  3. Set wssh = CreateObject("wscript.shell")
  4. Set fso = CreateObject("scripting.filesystemobject")
  5. Do Until vbstr = "exit"
  6. vbstr = InputBox("请输入")
  7. fso.OpenTextFile("temp.txt", 8, True).WriteLine vbstr
  8. Loop
  9. wssh.Run "temp.txt", 1, True
  10. Set wssh = Nothing
  11. Set fso = Nothing
复制代码
***共同提高***

TOP

大家还能帮忙吗

TOP

目前我在每个Case Else后加了sleep暂时延迟输入,还有其他的好办法吗?

TOP

原来有有一部分先执行了,当输入第一个对话框不为0时,它马上就开始执行了,第二,第三都是如此,可以等三个对话框全部输入完毕再开始执行吗?

TOP

8# fastslz
谢谢做的例子
我已经编辑运行成功,只是有些不足,请看我修改后的
  1. Dim tangshui,gansu,touxi,wshshell
  2. Set WshShell = CreateObject ("Wscript.Shell")
  3. input=InputBox("糖水瓶数")
  4. aSelect(input)
  5. input=InputBox("肝素支数")
  6. bSelect(input)
  7. input=InputBox("透析器次数")
  8. cSelect(input)
  9. Function aSelect(N)
  10.     Select Case N
  11.         Case 0
  12.              Exit Function
  13.         Case Else
  14.              Wscript.sleep 3000
  15.              Wshshell.SendKeys "lhn"
  16.              Wshshell.SendKeys "{ENTER}"
  17.              Wshshell.SendKeys "{ENTER}"
  18.              Wshshell.SendKeys ""&N&""
  19.              Wshshell.SendKeys "{ENTER}"
  20.              Wshshell.SendKeys "{ENTER}"
  21.              Wshshell.SendKeys "{ENTER}"
  22.     End Select
  23. End Function
  24. Function bSelect(B)
  25.     Select Case B
  26.         Case 0
  27.              Exit Function
  28.         Case Else
  29.              Wshshell.SendKeys "gsz"
  30.              Wshshell.SendKeys "{ENTER}"
  31.              Wshshell.SendKeys "{ENTER}"
  32.              Wshshell.SendKeys ""&B&""
  33.              Wshshell.SendKeys "{ENTER}"
  34.              Wshshell.SendKeys "{ENTER}"
  35.              Wshshell.SendKeys "{ENTER}"
  36.     End Select
  37. End Function
  38. Wshshell.SendKeys "xytx"
  39. Wshshell.SendKeys "{ENTER}"
  40. Wshshell.SendKeys "{ENTER}"
  41. Wshshell.SendKeys "{ENTER}"
  42. Wshshell.SendKeys "{ENTER}"
  43. Wshshell.SendKeys "{ENTER}"
  44. Wshshell.SendKeys "xtjc"
  45. Wshshell.SendKeys "{ENTER}"
  46. Wshshell.SendKeys "{ENTER}"
  47. Wshshell.SendKeys "{ENTER}"
  48. Wshshell.SendKeys "{ENTER}"
  49. Wshshell.SendKeys "{ENTER}"
  50. Wshshell.SendKeys "ycxg"
  51. Wshshell.SendKeys "{ENTER}"
  52. Wshshell.SendKeys "{ENTER}"
  53. Wshshell.SendKeys "{ENTER}"
  54. Wshshell.SendKeys "{ENTER}"
  55. Function cSelect(c)
  56.     Select Case c
  57.         Case 0
  58.              Exit Function
  59.         Case Else
  60.              Wshshell.SendKeys "txq"
  61.              Wshshell.SendKeys "{ENTER}"
  62.              Wshshell.SendKeys "{ENTER}"
  63.              Wshshell.SendKeys "{ENTER}"
  64.              Wshshell.SendKeys "{ENTER}"
  65.     End Select
  66. End Function
复制代码
1,运行后,在输入糖水瓶数后,第二个肝素的对话框要隔好几秒才出现,大家帮我提速下
2,当有透析器时,透析器在代码的最低端,希望它在输出时也在最低端。
默认顺序是
xytx
xtjc
ycxg
txq
现在的输出结果是
txq
xytx
xtjc
ycxg
  1. PS:输出结果请用TXT测试
复制代码

TOP

  1. Set WshShell = CreateObject ("Wscript.Shell")
  2. input = InputBox("第一次询问,请输入")
  3. aSelect(input)
  4. input = InputBox("第二次询问,请输入")
  5. aSelect(input)
  6. input = InputBox("第三次询问,请输入")
  7. aSelect(input)
  8. Function aSelect(N)
  9.     Select Case N
  10.         Case 0
  11.              Exit Function
  12.         Case Else
  13.              WshShell.SendKeys ""&N&""
  14.     End Select
  15. End Function
复制代码

TOP

返回列表