什么是正确的Shell函数代码来打开文件类型

时间:2015-09-13 21:06:47

标签: excel-vba vba excel

有人可以帮助解决下面的问题运行代码。它在Shell函数中只指定了1个文件名时有效,但是当我尝试循环时,我希望Shell只是一个指定文件类型的文件打开器(即.sim),系统无休止地循环;打开.exe并从打开的可执行程序“文件不存在”中显示一个对话框。

背景:我在文件夹中有很多.sim文件,我想使用下面循环中的代码执行执行。一旦第一个打开的.sim文件的任务完成,我想循环遍历所有剩余的.sim文件。

Xidgel我尝试了这个,它只运行一次,然后再次尝试失败;

Sub Test1()
Dim strProgramName As String
Dim Foldername As String
Dim Fname As String

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = False
Dim windowStyle As Integer: windowStyle = 1
strProgramName = "C:\userspath.exe"
Foldername = "C:\whatever\"
Fname = Dir(Foldername & "*.sim")

Do While Len(Fname)
wsh.Run strProgramName & " " & Foldername & Fname, windowStyle, waitOnReturn
Application.Wait Now + TimeValue("0:00:02")
SendKeys "(%)m"
Application.Wait Now + TimeValue("0:00:02")
SendKeys "{DOWN 13}"
Application.Wait Now + TimeValue("0:00:02")
SendKeys "{ENTER}"
Application.Wait Now + TimeValue("0:00:02")
SendKeys "{ENTER}"
Application.Wait Now + TimeValue("0:00:02")
SendKeys "(^S)"
Application.Wait Now + TimeValue("0:00:02")
SendKeys "%{F4}"
Fname = Dir()
Loop
MsgBox "Task Complete!"
End Sub

2 个答案:

答案 0 :(得分:0)

以下代码适用于我(将文本和命令发送到使用记事本编辑的一系列文件):

Public Sub test()
    Dim strProgramName As String
    Dim Foldername As String
    Dim Fname As String
    Dim TaskID As Variant

    strProgramName = "C:\Windows\system32\notepad.exe"
    Foldername = "C:\temp\"
    Fname = Dir(Foldername & "*.dat")
    Do While Len(Fname)
        ' Call Shell("""" & strProgramName & """ """ & Fname & """")
        TaskID = Shell(strProgramName & " " & Foldername & Fname, vbNormalFocus)
        AppActivate TaskID

        Application.SendKeys "ABC" & vbCr, True  ' Add some text
        Application.Wait Now + TimeValue("0:00:02")

        Application.SendKeys "^s", True  ' CTRL-s = save
        Application.Wait Now + TimeValue("0:00:02")

        Application.SendKeys "%{F4}", True  ' Alt-F4 = Quit
        Application.Wait Now + TimeValue("0:00:02")

        ' Get next file
        Fname = Dir()
    Loop
    MsgBox "Task Complete!"
End Sub

对我来说SendKeys的使用有点脆弱。我需要调用AppActivate以确保按键指向记事本。我首先尝试使用Shell命令而不使用vbNormalFocus,只有部分键盘通过记事本。此外,当我尝试从VBA环境运行代码时,键击被发送到Excel,因此我必须通过从Excel运行来进行测试。

希望这能让你开始。

答案 1 :(得分:0)

确定这是一个新版本,可以打开.exe 一次,打开/编辑/保存/关闭一系列文件,然后关闭.exe。我希望更接近解决方案。

Public Sub send_keys_test_2()
    Dim strProgramName As String
    Dim Foldername As String
    Dim Fname As String
    Dim TaskID As Variant

    ' In version 1 I tested with Notepad
    ' This version won't work with Notepad because it assumes
    ' a multiple document interface (MDI). Specifically, we want
    ' an .exe that can be open without having documents open. Notepad
    ' fails this requirement --- if you close a document in Notepad
    ' then the Notepad application closes too. So in this version
    ' I will test with MS Word.
    ' Modify to suit your purposes.
    strProgramName = "C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.exe"

    ' My test files are in C:\temp
    ' Modify to suit your purposes
    Foldername = "C:\temp\"

    ' My test files are a series of Word Docs
    ' Modify to suit your purposes
    Fname = Dir(Foldername & "File*.doc")

    ' If there are no matching files, then exit
    If Len(Fname) = 0 Then Exit Sub

    ' Otherwise, start the .exe WITHOUT opening any files
    TaskID = Shell(strProgramName, vbNormalFocus)

    ' Allow plenty of time for the .exe to open
    Application.Wait Now + TimeValue("0:00:10")

    ' Make sure the keystrokes get sent to the .exe
    AppActivate TaskID

    Do While Len(Fname)
        ' Call Shell to open the first file
        ' In Word, send CTRL-o to display the file open dialog box
        ' Then send the Foldername + FName
        ' Then send an ENTER key to complete the file open
        ' Modify this to suit your purposes
        Application.SendKeys "^o", True                 ' CTRL-o = open
        Application.Wait Now + TimeValue("0:00:02")
        Application.SendKeys Foldername & Fname, True   ' Send the file name
        Application.SendKeys "~", True                  ' Send {Enter} to close dialog, open file

        ' Now edit the file
        ' For demo purposes, just send a few new chars
        Application.SendKeys "ABC" & vbCr, True         ' Add some text

        ' Save the file
        ' In Word, send CTRL-s
        ' Modify to suit your purposes
        Application.SendKeys "^s", True  ' save

        ' Close the file
        ' In Word, send CTRL-w
        ' Modify to suit your purposes
        Application.SendKeys "^w", True  ' save

        ' Get next file
        Fname = Dir()
        Application.Wait Now + TimeValue("0:00:02")
    Loop

    ' Send the quit command
    ' In Word, send Alt-F4
    ' Modify to suit your purposes
    AppActivate TaskID
    Application.Wait Now + TimeValue("0:00:02")
    Application.SendKeys "%{F4}", True  ' Alt-F4 = Quit

    MsgBox "Task Complete!"
End Sub

希望这有帮助。