I am trying to unzip a .tar file using VBA. I have googled it to find the answer, but not many article talk about the unzipped . tar file.
The code I refer is below : from here: https://www.rondebruin.nl/win/s7/win002.htm because I want to unzipped the .tar file.
Step 1, I changed the below code to from .zip to .tar or .tar.gz
Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
MultiSelect:=False)
it found the file, but I failed in below line:
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
The error was:
run-time error'-214767259(80004005)':Method 'NameSpace' of object 'IsheLLdispatch6' failed.
Below the code I refer for unzipped zip file.
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
Edit:
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