Reputation: 43
I'm trying to unzip a docx via command line from VBA, is it possible to extract the contents of the word file i.e. the folder structure when extracting to a custom path.
If i used "7z e" command I'm able to extract to a custom path but the folder structure is lost.
If i used "7z x" command, the folder structure is maintained but the extraction is done under "User folder"
Any suggestion would be most helpful,
Thanks in advance.
Code:
Sub UnZip7Zip(strTargetPath As String, Fname As Variant, Fext As Variant)
Dim oApp As Object, FSOobj As Object, fol, objFiles As Object, lngFileCount As Long, Source As String, Destination As String
Dim MyFile As String, Outdir As String, Cmdstr As String
Dim FileNameFolder As Variant
If Right(strTargetPath, 1) <> Application.PathSeparator Then
strTargetPath = strTargetPath & Application.PathSeparator
End If
FileNameFolder = strTargetPath & Fname
'create destination folder if it does not exist
Set FSOobj = CreateObject("Scripting.FilesystemObject")
If FSOobj.FolderExists(FileNameFolder) = False Then
FSOobj.CreateFolder FileNameFolder
End If
Set objFiles = FSOobj.GetFolder(strTargetPath & Fname).Files
lngFileCount = objFiles.count
If (lngFileCount = 0) Then
'Prompts whether to overwrite if files exist ' This extracts all files into a single folder
Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " e " & """" & MyFile & """" & " -o" & """" & Outdir & """"
'This command is working wen directly give in cmd prmpt
'Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " x " & """" & MyFile & """"
ElseIf (lngFileCount > 0) Then
'Replace files in the destination
Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " e " & """" & MyFile & """" & "-aoa" & " -o" & """" & Outdir & """"
'This command is working wen directly give in cmd prmpt
'Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " x " & """" & MyFile & """" & "-aoa"
End If
Debug.Print Cmdstr
'MsgBox Cmdstr
'Extract to the specified path
Call Shell(Cmdstr, 1) ' Here i extract the zip file
'CommandLine Cmdstr, True
Set oApp = Nothing
Set FSOobj = Nothing
Set FileNameFolder = Nothing
End Sub
Upvotes: 2
Views: 2567
Reputation: 30113
In your code:
7za.exe
rather than 7z.exe
Dim MyFile As String, Outdir As String
statement declares
variables MyFile
and Outdir
(and allocates storage space), but I
don't see those variables are defined in your code (where)... & "-aoa" & ...
, should be ... & " -aoa" & ...
Upvotes: 3