本帖最后由 ygqiang 于 2015-1-21 18:47 编辑
vbs代码,如果双击多次,运行了多次。就可能出现错误。如何避免?
保证双击多次vbs,也只运行1次。
可以在vbs代码前面,加入个检测功能:
1、如果存在c:\tt.tt文件,就直接退出。
2、如果不存在,就建立c:\tt.tt文件,并执行后面的vbs代码- Do
- JK
- Loop
-
- '一直检查窗口标题
- Sub JK()
- Dim wso,strTitle
- strTitle = "Microsoft Windows"
- Set wso = CreateObject("Wscript.Shell")
-
- ' 一直检查窗口标题
- Do While wso.AppActivate(strTitle) = False
- WScript.sleep 200 ' 延时 0.2 秒
- Call guan()
- Loop
-
- WScript.Sleep 500 ' 延时 0.5 秒
- Call cunz()
- wso.SendKeys "(%{F4})" ' 发送 Alt + F4
- wso.Run "Explorer.exe /n," '打开我的电脑
- WScript.Sleep 500 ' 延时 0.5 秒
- Call guan()
- Set wso = NoThing
- End Sub
-
-
- '关闭重复窗口
- Sub guan()
- Set Shell = CreateObject("Shell.Application")
- Set Dict = CreateObject("Scripting.Dictionary")
- Set Wins = Shell.Windows
- For i=Wins.Count-1 To 0 step -1
- If Instr(LCase(Wins(i).FullName),"\explorer.exe") Then
- If Dict.Exists(Wins(i).LocationURL) Then
- Wins(i).Quit
- Else
- Dict.Add Wins(i).LocationURL,True
- End If
- End If
- Next
- End Sub
-
-
- '激活窗口
- Sub cunz()
- set wshell = CreateObject("word.Application")
- set wshellw = wshell.tasks
- na="Microsoft Windows"
- If wshellw.Exists(na) Then
- wshellw(na).Activate '激活窗口
- wshellw(na).WindowState = 0 '0平常模式、1最大化模式、2最小化模式
- End If
- End Sub
复制代码
|