我正在尝试使用VBA解压缩.tar文件。我用谷歌搜索了答案,但是关于解压缩的文章很少。 tar文件。
我引用的代码如下:从这里:https://www.rondebruin.nl/win/s7/win002.htm,因为我想解压缩.tar文件。
步骤1,我将以下代码从.zip更改为.tar或.tar.gz
Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
MultiSelect:=False)
它找到了文件,但是我在下面的行中失败了:
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
错误是:
运行时错误'-214767259(80004005)':对象'IsheLLdispatch6'的方法'NameSpace'失败。
在我引用的未压缩zip文件的代码下面。
Sub Unzip3()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
MultiSelect:=False)
If Fname = False Then
'Do nothing
Else
'Destination folder
DefPath = "C:\Users\Ron\test\" '<<< Change path
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
FileNameFolder = DefPath
' 'Delete all the files in the folder DefPath first if you want
' On Error Resume Next
' Kill DefPath & "*.*"
' On Error GoTo 0
'Extract the files into the Destination folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub
编辑:
Sub ExtractAllFiles()
Dim File As String
Dim ShellStr
File = Dir("C:\test")
While (File <>"")
if Instr(1,File,".tar")>0 then
ShellStr = "C:\Program Files\PKWARE\PKZIPW -e C:\test\ " & File & _
" C:\test\"
Call Shell(ShellStr, vbHide)
End if
File = Dir
DoEvents
Loop
End Sub
您需要引用文件路径(并可能包含可执行文件的名称)
Sub ExtractAllFiles()
Dim File As String
Dim ShellStr
File = Dir("C:\test\*.tar")
While (File <>"")
ShellStr = """C:\Program Files\PKWARE\PKZIPW"" -e ""C:\test\ " & File & _
""" ""C:\test\"""
Call Shell(ShellStr, vbHide)
File = Dir
DoEvents
Loop
End Sub