问题
I have the following VBA code to extract all the files within a given directory.
Sub extractAllFiles()
Dim MyObj As Object, MySource As Object, file As Variant
Dim shellStr As String
file = Dir("C:\Downloads\")
While (file <> "")
If InStr(file, ".gz") > 0 Then
shellStr = "winzip32 -e C:\Downloads\" & file & " C:\Downloads\"
Call Shell(shellStr, vbHide)
End If
file = Dir
Wend
End Sub
When I execute this sub routine I get a Run-Time error 53, "File Not Found" error. When I copy the shellStr... Example: winzip32 -e C:\Downloads\file1.gz C:\Downloads\
and execute it from command prompt, it works perfectly! I get the text file within file1.gz extracted to the downloads directory. However running it from VBA doesn't seem to work.
Can anyone shed some light?
回答1:
You should try with the full path of the shell command, like this, that worked for me:
Sub extractAllFiles()
Dim MyObj As Object, MySource As Object, file As Variant
Dim shellStr As String
file = Dir("C:\Downloads\")
While (file <> "")
If InStr(1, file, ".gz") > 0 Then
shellStr = "C:\Program Files (x86)\WinZip\winzip32 -e C:\Downloads\" & file & " C:\Downloads\"
Call Shell(shellStr, vbHide)
End If
file = Dir
Wend
End Sub
My winzip is installed as C:\Program Files (x86)\WinZip\winzip32. You should use yours. Your install path may be:
C:\Program Files\WinZip\winzip32
回答2:
WinZip path needs to be absolute path:
C:\Program Files\WinZip\winzip32'
回答3:
Check your WinZip path. That needs to be fine fixed to the full path to your WinZip.
回答4:
Using the logic from this SO post, try the below code:
Sub ExtractAllFiles()
Dim FileName As String
Dim ShellStr
FileName = Dir("C:\Downloads\*.gz")
Do While Len(FileName) > 0
ShellStr = "winzip32 -e " & FileName & " C:\Downloads"
Call Shell(ShellStr, vbHide)
FileName = Dir
Loop
End Sub
Let us know if this helps.
来源:https://stackoverflow.com/questions/20222353/extract-all-gz-file-in-folder-using-vba-shell-command