在使用Shell 去调用一个可执行文件是,居然开启程式后自动关闭,不得不寻找其它的方法去解决。
1. 使用SendKeys开启Win键,再送键”R”,开启“运行”,等待100us,给运行窗口送一串字符,回车。
OptionExplicit
PrivateDeclare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByValbScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
PrivateDeclare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const VK_LWIN = &H5B '左WIN键
Private Const VK_RWIN = &H5C '右WIN键
PrivateSub Form_Load()
keybd_eventVK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0 ' 按下
keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0 ' 弹起
Sleep (50)
SendKeys "r", True
Sleep (50)
SendKeys "\\192.168.2.42", True
'此例是登入服务网络,可以使用其它的可执行文件,例如"C:\Program Files\Google\Chrome\Application\chrome.exe"
Sleep (50)
SendKeys"{ENTER}"
End Sub
2.使用句柄’开启运行窗口
Dim objshell
Set objshell = CreateObject("shell.application")
objshell.filerun
Delaytime (1000)
lRunHwnd = FindWindow(vbNullString, "运行")
,查找到运行窗口的句柄
While lRunHwnd = 0
Delaytime (100)
lRunHwnd = FindWindow(vbNullString, "运行")
Debug.Print lRunHwnd
Wend
‘查找到运行窗口的组合框
lComBOHwnd = FindWindowEx(lRunHwnd, 0, "ComboBox", vbNullString)
lTxtHwnd = FindWindowEx(lComBOHwnd, 0, "Edit", vbNullString)
’给TEXT文本框传递字符串,即要执行的程式及路径
SendMessage lTxtHwnd, WM_SETTEXT, 255, ByVal "C:\Program Files\Intel Corporation\Intel(R) Thermal Analysis Tool\tat.exe"
lCmdHwnd = FindWindowEx(lRunHwnd, 0, vbNullString, "确定")
SendMessage lCmdHwnd, WM_LBUTTONDOWN, ByVal 0&, ByVal 0&
SendMessage lCmdHwnd, WM_LBUTTONUP, ByVal 0&, ByVal 0&