标题: [问题求助] [已解决]VBS怎样根据输入的数值运行不同的按键动作? [打印本页]
作者: qiaoliangih 时间: 2011-5-5 19:48 标题: [已解决]VBS怎样根据输入的数值运行不同的按键动作?
本帖最后由 qiaoliangih 于 2011-5-11 00:29 编辑
本人在医院工作,经常输入一些重复的药名与化验
所以做了一个vbs,全部用WshShell.SendKeys写的。但功能不完善
有些病号用药多,有些少,化验也是如此,我不得的不每次运行完vbs后修改录入记录。
我想让大家帮忙写个高级一点的。
运行vbs后先会询问这个药需要几个,按照输入的值去输入药量。
然后第二次询问另外一种药,如果是0就不输入,如果是1就输入1,是2就是输入2。
第三次询问有无特殊化验(默认是输出3种化验),如是0,则不输出特殊化验,如果是1,就输出特殊化验。
请大家帮帮忙,谢谢
作者: 冷玉公子 时间: 2011-5-5 20:27
批处理不能调用按键的,不过你可以用批处理调用VBS的WshShell.SendKeys动作
如果你对批处理有了解过的话 就是连个命令就可以解决你的问题
用 Set /P = 命令等待输入值 然后用 IF 命令去判断这个值 然后调用相应的VBS即可
例如- @echo off
- Echo 1 是我 2 是你
- Set /P Var= 请输入值:
- if "%Var%" == "1" goto :1
- if "%Var%" == "2" goto :2
- :1
- Echo 是我
- :2
- Echo 是你
复制代码
就是这样的思路了
作者: qiaoliangih 时间: 2011-5-6 00:21
可否改用vbs来做判断?
作者: qiaoliangih 时间: 2011-5-6 00:40
类似于这种- Dim name,sex,birth
- name=InputBox("请输入姓名")
- sex=InputBox("请输入姓别")
- birth=InputBox("请输入出生日期")
- Set fso=CreateObject("scripting.filesystemobject")
- Set file=fso.createtextfile("" & name & ".txt",True)
- file.Close
- MsgBox name
- set fi=fso.opentextfile("" & name & ".txt",2)
- s="姓名:"&name&vbCrLf&"姓别:"&sex&vbCrLf&"出生日期:"&birth
- fi.write s
- fi.close
复制代码
作者: batman 时间: 2011-5-6 08:49
本帖最后由 batman 于 2011-5-6 09:23 编辑
还是用数组来处理吧,下面给个示例,楼主对照着修改罗- '假如有4种药,每种药有3种剂量,每种药有2种化验
- Dim chufan(3, 2, 1), a, b, c, name, number, test
- namearray = Array("头胞", "葡萄糖", "阿期匹林", "刺五加")
- numberarray = Array("2ml", "4ml", "6ml")
- testarray = Array("甲类", "乙类")
- For a = 0 To 3
- name = name & a+1 & ":" & namearray(a) & " "
- For b = 0 To 2
- If a = 0 Then number = number & b+1 & ":" & numberarray(b) & " "
- For c = 0 To 1
- If a & b = "00" Then test = test & c+1 & ":" & testarray(c) & " "
- chufan(a, b, c) = namearray(a) & "-" & numberarray(b) & "-" & testarray(c)
- Next
- Next
- Next
- input = InputBox(name & vbcrlf & number & vbcrlf & test & vbcrlf & "请分别输入药品、剂量、化验的序号,中间请用-符号格开:")
- inputsplit = Split(input, "-")
- MsgBox chufan(inputsplit(0)-1, inputsplit(1)-1, inputsplit(2)-1)
复制代码
作者: qiaoliangih 时间: 2011-5-6 19:10
5# batman
谢谢版主大大
我想问下如何用if判断值呢?- 第一种药是必须有的,输入多少数值,就模拟键盘输入相应的数量
- 第二种药是非必须的,有人要,就输入相应数值,然后用模拟键盘输入相应的数量。要是不要这个,就输入0,就不调用模拟键盘。也就是不输入。
- 第三个是化验,有人要,就输入相应数值,然后用模拟键盘输入相应的数量。要是不要这个,就输入0,就不调用模拟键盘。也就是不输入。
复制代码
if gansu = "0" then
end if
if gansu = ">0" then
WshShell.SendKeys ""&gansu&""
if touxi = "0" then
end if
if touxi = ">0" then
WshShell.SendKeys ""&touxi&""
作者: qiaoliangih 时间: 2011-5-6 19:16
- Dim tangshui,gansu,touxi
-
- tangshui=InputBox("瓶数")
-
- gansu=InputBox("支数")
-
- touxi=InputBox("次数")
- if gansu = "0" then
- end if
- if gansu = ">0" then
- WshShell.SendKeys ""&gansu&""
-
- if touxi = "0" then
- end if
- if touxi = ">0" then
- WshShell.SendKeys ""&touxi&""
复制代码
这样的结构可以实现吗?
作者: fastslz 时间: 2011-5-6 20:15
- Set WshShell = CreateObject ("Wscript.Shell")
- input = InputBox("第一次询问,请输入")
- aSelect(input)
-
- input = InputBox("第二次询问,请输入")
- aSelect(input)
-
- input = InputBox("第三次询问,请输入")
- aSelect(input)
-
- Function aSelect(N)
- Select Case N
- Case 0
- Exit Function
- Case Else
- WshShell.SendKeys ""&N&""
- End Select
- End Function
复制代码
作者: qiaoliangih 时间: 2011-5-7 01:51
8# fastslz
谢谢做的例子
我已经编辑运行成功,只是有些不足,请看我修改后的- Dim tangshui,gansu,touxi,wshshell
- Set WshShell = CreateObject ("Wscript.Shell")
- input=InputBox("糖水瓶数")
- aSelect(input)
- input=InputBox("肝素支数")
- bSelect(input)
- input=InputBox("透析器次数")
- cSelect(input)
-
- Function aSelect(N)
- Select Case N
- Case 0
- Exit Function
- Case Else
- Wscript.sleep 3000
- Wshshell.SendKeys "lhn"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys ""&N&""
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
-
- End Select
- End Function
-
- Function bSelect(B)
- Select Case B
- Case 0
- Exit Function
- Case Else
- Wshshell.SendKeys "gsz"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys ""&B&""
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- End Select
- End Function
-
- Wshshell.SendKeys "xytx"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
-
- Wshshell.SendKeys "xtjc"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
-
- Wshshell.SendKeys "ycxg"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Function cSelect(c)
- Select Case c
- Case 0
- Exit Function
- Case Else
- Wshshell.SendKeys "txq"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- Wshshell.SendKeys "{ENTER}"
- End Select
- End Function
复制代码
1,运行后,在输入糖水瓶数后,第二个肝素的对话框要隔好几秒才出现,大家帮我提速下
2,当有透析器时,透析器在代码的最低端,希望它在输出时也在最低端。
默认顺序是
xytx
xtjc
ycxg
txq
现在的输出结果是
txq
xytx
xtjc
ycxg复制代码
。
作者: qiaoliangih 时间: 2011-5-7 10:48
原来有有一部分先执行了,当输入第一个对话框不为0时,它马上就开始执行了,第二,第三都是如此,可以等三个对话框全部输入完毕再开始执行吗?
作者: qiaoliangih 时间: 2011-5-7 12:12
目前我在每个Case Else后加了sleep暂时延迟输入,还有其他的好办法吗?
作者: qiaoliangih 时间: 2011-5-7 18:17
大家还能帮忙吗
作者: batman 时间: 2011-5-7 22:52
12# qiaoliangih
非要用sendkey,直接输入不行?- '输入exit退出循环
- Dim vbstr, wssh, fso
- Set wssh = CreateObject("wscript.shell")
- Set fso = CreateObject("scripting.filesystemobject")
- Do Until vbstr = "exit"
- vbstr = InputBox("请输入")
- fso.OpenTextFile("temp.txt", 8, True).WriteLine vbstr
- Loop
- wssh.Run "temp.txt", 1, True
- Set wssh = Nothing
- Set fso = Nothing
复制代码
作者: qiaoliangih 时间: 2011-5-8 01:15
13# batman
恩,是需要Wshshell.SendKeys的,因为是要输入另外一个软件的。目前只有vbs模拟键盘比较有效
作者: fastslz 时间: 2011-5-8 11:03
8楼顺序问题,你自己解决吧
就代码不稳定问题我给你一些参考
vbs模拟键盘最不稳定有2大问题,当前窗体及延时,延时还取决于硬件性能
在指定的窗体发送模拟键- '这个下面的例子激活“计算器”窗体发送1+1=模拟键
- Set WshShell = Wscript.CreateObject("Wscript.Shell")
- WshShell.AppActivate "计算器"
- WshShell.SendKeys "1{+}1~"
复制代码
上面的还有点欠缺不能在"计算器"最小化时不能将其激活- '这个例子需要安装有office 2003以上的系统中运行,
- '如果有“计算器”窗体就激活该窗体,激活后将其最大化并发送1+1=模拟键
- Set WshShell = Wscript.CreateObject("Wscript.Shell")
- Set objWord = CreateObject("Word.Application")
- Set colTasks = objWord.Tasks
- If colTasks.Exists("计算器") Then
- colTasks("计算器").Activate
- colTasks("计算器").WindowState = 1
- WshShell.SendKeys "1{+}1~"
- End If
- objWord.Quit
复制代码
作者: qiaoliangih 时间: 2011-5-9 11:20
现在我陷入困境了,彻底没有招了
作者: fastslz 时间: 2011-5-10 13:25
本帖最后由 fastslz 于 2011-5-10 15:01 编辑
- Dim tangshui,gansu,touxi,wshshell,aStrs,bStrs,cStrs
- Set WshShell = CreateObject ("Wscript.Shell")
- input=InputBox("糖水瓶数")
- aSelect(input)
- input=InputBox("肝素支数")
- bSelect(input)
- input=InputBox("透析器次数")
- cSelect(input)
-
- If Not aStrs = "" Then
- KeysNames = Split(aStrs, ",")
- For i = 0 to UBound(KeysNames)
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- If Not bStrs = "" Then
- KeysNames = Split(bStrs, ",")
- For i = 0 to UBound(KeysNames)
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- Wshshell.SendKeys "xytx"
- Wscript.sleep 20 '若想连续模拟按键稳定性,每个模拟按键延时20毫秒,延时多少取决于你的计算机性能
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
-
- Wshshell.SendKeys "xtjc"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
-
- Wshshell.SendKeys "ycxg"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
-
- If Not cStrs = "" Then
- KeysNames = Split(cStrs, ",")
- For i = 0 to UBound(KeysNames)
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- Function aSelect(N)
- Select Case N
- Case 0
- aStrs = ""
- Exit Function
- Case Else
- aStrs = "lhn,{ENTER},{ENTER}," & N & ",{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
-
- Function bSelect(B)
- Select Case B
- Case 0
- bStrs = ""
- Exit Function
- Case Else
- bStrs = "gsz,{ENTER},{ENTER}," & B & ",{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
-
- Function cSelect(c)
- Select Case c
- Case 0
- cStrs = ""
- Exit Function
- Case Else
- cStrs = "txq,{ENTER},{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
复制代码
下面的代码更稳定,但必须安装word,例子为循环等待记事本窗体,发现窗体就模拟按键,仅供参考- Dim tangshui,gansu,touxi,wshshell,aStrs,bStrs,cStrs
- Set WshShell = CreateObject ("Wscript.Shell")
- input=InputBox("糖水瓶数")
- aSelect(input)
- input=InputBox("肝素支数")
- bSelect(input)
- input=InputBox("透析器次数")
- cSelect(input)
-
- If Not aStrs = "" Then
- KeysNames = Split(aStrs, ",")
- For i = 0 to UBound(KeysNames)
- aWindow("记事本")
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- If Not bStrs = "" Then
- KeysNames = Split(bStrs, ",")
- For i = 0 to UBound(KeysNames)
- aWindow("记事本")
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- aWindow("记事本")
- Wshshell.SendKeys "xytx"
- Wscript.sleep 20 '若想连续模拟按键稳定性,每个模拟按键延时20毫秒,延时多少取决于你的计算机性能
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
-
- aWindow("记事本")
- Wshshell.SendKeys "xtjc"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
-
- aWindow("记事本")
- Wshshell.SendKeys "ycxg"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
- Wscript.sleep 20
- Wshshell.SendKeys "{ENTER}"
-
- If Not cStrs = "" Then
- KeysNames = Split(cStrs, ",")
- For i = 0 to UBound(KeysNames)
- aWindow("记事本")
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- Function aSelect(N)
- Select Case N
- Case 0
- aStrs = ""
- Exit Function
- Case Else
- aStrs = "lhn,{ENTER},{ENTER}," & N & ",{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
-
- Function bSelect(B)
- Select Case B
- Case 0
- bStrs = ""
- Exit Function
- Case Else
- bStrs = "gsz,{ENTER},{ENTER}," & B & ",{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
-
- Function cSelect(c)
- Select Case c
- Case 0
- cStrs = ""
- Exit Function
- Case Else
- cStrs = "txq,{ENTER},{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
-
- Sub aWindow(WindowT)
- Set objWord = CreateObject("Word.Application")
- Set colTasks = objWord.Tasks
- Do
- If colTasks.Exists(WindowT) Then
- colTasks(WindowT).Activate
- colTasks(WindowT).WindowState = 0
- Exit Do
- End If
- Wscript.sleep 200
- Loop
- objWord.Quit
- End Sub
复制代码
作者: fastslz 时间: 2011-5-10 13:37
本帖最后由 fastslz 于 2011-5-10 14:59 编辑
再给你一个方法- Dim tangshui,gansu,touxi,wshshell,aStrs,bStrs,cStrs,dStrs
- Set WshShell = CreateObject ("Wscript.Shell")
- input=InputBox("糖水瓶数")
- aSelect(input)
- input=InputBox("肝素支数")
- bSelect(input)
- input=InputBox("透析器次数")
- cSelect(input)
-
- If Not aStrs = "" Then
- KeysNames = Split(aStrs, ",")
- WshShell.AppActivate "程序窗体名称" '用于激活你的程序窗体,不至于在其他窗体上乱按
- For i = 0 to UBound(KeysNames)
- Wscript.sleep 20
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- If Not bStrs = "" Then
- KeysNames = Split(bStrs, ",")
- WshShell.AppActivate "程序窗体名称" '用于激活你的程序窗体,不至于在其他窗体上乱按
- For i = 0 to UBound(KeysNames)
- Wscript.sleep 20
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- dStrs = "xytx,{ENTER},{ENTER},{ENTER},{ENTER},{ENTER},xtjc,{ENTER},{ENTER},{ENTER},{ENTER},{ENTER},ycxg,{ENTER},{ENTER},{ENTER},{ENTER}"
- If Not dStrs = "" Then
- KeysNames = Split(dStrs, ",")
- WshShell.AppActivate "程序窗体名称" '用于激活你的程序窗体,不至于在其他窗体上乱按
- For i = 0 to UBound(KeysNames)
- Wscript.sleep 20
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- If Not cStrs = "" Then
- KeysNames = Split(cStrs, ",")
- WshShell.AppActivate "程序窗体名称" '用于激活你的程序窗体,不至于在其他窗体上乱按
- For i = 0 to UBound(KeysNames)
- Wscript.sleep 20
- Wshshell.SendKeys KeysNames(i)
- Next
- End If
-
- Function aSelect(N)
- Select Case N
- Case 0
- aStrs = ""
- Exit Function
- Case Else
- aStrs = "lhn,{ENTER},{ENTER}," & N & ",{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
-
- Function bSelect(B)
- Select Case B
- Case 0
- bStrs = ""
- Exit Function
- Case Else
- bStrs = "gsz,{ENTER},{ENTER}," & B & ",{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
-
- Function cSelect(c)
- Select Case c
- Case 0
- cStrs = ""
- Exit Function
- Case Else
- cStrs = "txq,{ENTER},{ENTER},{ENTER},{ENTER}"
- End Select
- End Function
复制代码
作者: qiaoliangih 时间: 2011-5-11 00:28
17# fastslz
18# fastslz
谢谢,谢谢fastslz 大大给出的完美解决办法,实在是太感谢了!!!!!!
作者: zhangop9 时间: 2011-10-1 17:24
这个好用,留个记号!!!!
作者: qiaoliangih 时间: 2011-12-1 17:53
该脚本我已经使用了大半年,今日想起,心存感激,特来再次感谢
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |