这个问题可能有点老了,但我想这个答案仍然可以提供帮助。 (使用Excel VBA测试,无法使用Access进行测试)
WshShell.Exec方法允许使用.StdIn,.StdOut和.StdErr函数来写入和读取CONSOL窗口。 WshShell.Run方法不允许使用此功能,因此在某些情况下需要使用Exec。
尽管确实没有内置函数来启动最小化或隐藏的Exec方法,但您可以使用API快速查找Exec窗口hwnd并最小化/隐藏它。
我的下面的脚本使用Exec对象的ProcessID来查找窗口的Hwnd。使用Hwnd,您可以设置窗口的显示状态。
从我使用Excel 2007 VBA进行测试,在大多数情况下,我甚至没有看到窗口......在某些情况下,它可能会在几毫秒内可见,但只会出现快速闪烁或闪烁......注意:使用SW_MINIMIZE比使用SW_HIDE有更好的结果,但你可以玩弄它。
我添加了TestRoutine Sub以显示如何使用'HideWindow'函数的示例。 'HideWindow'函数使用'GetHwndFromProcess'函数从ProcessID获取窗口hwnd。
放置下面成一个模块...
Option Explicit
' ShowWindow() Commands
Public Const SW_HIDE = 0
Public Const SW_MINIMIZE = 6
'GetWindow Constants
Public Const GW_CHILD = 5
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
' API Functions
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32"() As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Sub TestRoutine()
Dim objShell As Object
Dim oExec As Object
Dim strResults As String
Set objShell = CreateObject("WScript.Shell")
Set oExec = objShell.Exec("CMD /K")
Call HideWindow(oExec.ProcessID)
With oExec
.StdIn.WriteLine "Ping 127.0.0.1"
.StdIn.WriteLine "ipconfig /all"
.StdIn.WriteLine "exit"
Do Until .StdOut.AtEndOfStream
strResults = strResults & vbCrLf & .StdOut.ReadLine
DoEvents
Loop
End With
Set oExec = Nothing
Debug.Print strResults
End Sub
Function HideWindow(iProcessID)
Dim lngWinHwnd As Long
Do
lngWinHwnd = GetHwndFromProcess(CLng(iProcessID))
DoEvents
Loop While lngWinHwnd = 0
HideWindow = ShowWindow(lngWinHwnd, SW_MINIMIZE)
End Function
Function GetHwndFromProcess(p_lngProcessId As Long) As Long
Dim lngDesktop As Long
Dim lngChild As Long
Dim lngChildProcessID As Long
On Error Resume Next
lngDesktop = GetDesktopWindow()
lngChild = GetWindow(lngDesktop, GW_CHILD)
Do While lngChild <> 0
Call GetWindowThreadProcessId(lngChild, lngChildProcessID)
If lngChildProcessID = p_lngProcessId Then
GetHwndFromProcess = lngChild
Exit Do
End If
lngChild = GetWindow(lngChild, GW_HWNDNEXT)
Loop
On Error GoTo 0
End Function
ShowWindow函数: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx
GetWindow功能: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515%28v=vs.85%29.aspx
GetDesktopWindow功能: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx
GetWindowThr eadProcessId功能: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522%28v=vs.85%29.aspx
如果您需要更多关于API工作方式的信息,快速谷歌搜索将为您提供大量信息。
我希望这可以帮助...谢谢。
可能重复(http://stackoverflow.com/questions/15128517/want-to-hide-command -prompt-window-in-using-wshshell-exec-method) –
看到了这种方法,但没有奢望重定向到一个文件 - 多个用户。希望保持stdout方法。将继续寻找thx –
回复:“没有奢望重定向到一个文件 - 多个用户” - 这是将从数据库前端执行的事情,你***是确保每个用户都有他们自己的本地副本前端,对吧......? –