EXCEL Shell FINDSTR -不希望弹出DOS窗口出现

tct7dpnv  于 11个月前  发布在  Shell
关注(0)|答案(1)|浏览(86)

Windows 10我在EXCEL宏中有VB代码,它在文本文件中搜索值并返回匹配项。问题是在执行时弹出DOS窗口。我不希望出现此窗口(或者如果它必须出现,在某处最小化)。我做了很多宏编码,但不是真正的技术,我知道。这是我的代码,这是工作,但显示DOS窗口。它确实情况在文本文件中insensitve搜索USERNAME值。我需要得到的结果,并通过他们阅读。所有帮助赞赏,但我希望它不是太技术或激烈。谢谢!

theCommand = "findstr /I /P "
theCommand = theCommand & " " & USERNAME & " " & ACTIVEEMPLOYEESLISTFILE

Set oShell = Nothing
Set oExec = Nothing
Set oOutput = Nothing
        
Set oShell = CreateObject("WScript.Shell")

Set oExec = oShell.Exec(theCommand)
Set oOutput = oExec.StdOut

While Not oOutput.AtEndOfStream
    recordLine = oOutput.readline
    MsgBox recordLine
Wend

字符串
我只是想DOS窗口不出现或最小化时,运行,并能够读取的结果(这是不是在一个文件返回)。

k97glaaz

k97glaaz1#

这似乎对我有用:

Sub TestSearch()
    
    Const OUTPUT_FILE As String = "C:\Temp\output.txt" 'file for results
    '2>&1 - capture stderr in same file as stdout
    Const CMD As String = "cmd.exe /c findstr /I /P ""<lookfor>"" ""<infile>"" > ""<resultsfile>"" 2>&1"
    Dim runThis As String
    'build the command by replacing tokens
    runThis = Replace(CMD, "<lookfor>", "failed")
    runThis = Replace(runThis, "<infile>", "C:\Temp\tester.log")
    runThis = Replace(runThis, "<resultsfile>", OUTPUT_FILE)
    Debug.Print runThis
    With CreateObject("WScript.Shell")
        .Run runThis, 0, True
    End With
    
    Debug.Print FileText(OUTPUT_FILE) 'show the results
    
End Sub

'return all the text from a file
Function FileText(pth)
    With CreateObject("scripting.filesystemobject").OpenTextFile(pth)
        FileText = .readall()
        .Close
    End With
End Function

字符串

相关问题