0
I am doing a VBA macro in excel, to extract . zip files (which would be a barbada to do), except when the file is protected. I searched everything that is corner, there are several tutorials to extract . zip protected, but none of them is for when the file have password. So I found some tips to use Shell commands (Sorry, but I never used Shell in VBA), so I found the following code, and I can’t :
Public Sub ShellAndWait(ByVal PathName As String, Optional WindowState)
Dim hProg As Long
Dim hProcess As Long, ExitCode As Long
'fill in the missing parameter and execute the program
If IsMissing(WindowState) Then WindowState = 1
hProg = Shell(PathName, WindowState)
'hProg is a "process ID under Win32. To get the process handle:
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, hProg)
Do
'populate Exitcode variable
GetExitCodeProcess hProcess, ExitCode
DoEvents
Loop While ExitCode = STILL_ACTIVE
End Sub
'With this example you browse to the zip or 7z file you want to unzip
'The zip file will be unzipped in a new folder in: Application.DefaultFilePath
'Normal if you have not change it this will be your Documents folder
'The name of the folder that the code create in this folder is the Date/Time
'You can change this folder to this if you want to use a fixed folder:
'NameUnZipFolder = "C:\Users\Ron\TestFolder\"
'Read the comments in the code about the commands/Switches in the ShellStr
'There is no need to change the code before you test it
Sub A_UnZip_Zip_File_Browse()
Dim PathZipProgram As String, NameUnZipFolder As String
Dim FileNameZip As Variant, ShellStr As String
'Path of the Zip program
PathZipProgram = "C:\program files\7-Zip\"
If Right(PathZipProgram, 1) <> "\" Then
PathZipProgram = PathZipProgram & "\"
End If
'Check if this is the path where 7z is installed.
If Dir(PathZipProgram & "7zFM.exe") = "" Then
MsgBox "Please find your copy of 7z.exe and try again"
Exit Sub
End If
'Create path and name of the normal folder to unzip the files in
'In this example we use: Application.DefaultFilePath
'Normal if you have not change it this will be your Documents folder
'The name of the folder that the code create in this folder is the Date/Time
NameUnZipFolder = Application.DefaultFilePath & "\TESTE"
'You can also use a fixed path like
'NameUnZipFolder = "C:\Users\Ron\TestFolder"
'Select the zip file (.zip or .7z files)
FileNameZip = Application.GetOpenFilename(filefilter:="Zip Files, *.zip", _
MultiSelect:=False, Title:="Select the file that you want to unzip")
'Unzip the files/folders from the zip file in the NameUnZipFolder folder
If FileNameZip = False Then
MsgBox FileNameZip
Else
'There are a few commands/Switches that you can change in the ShellStr
'We use x command now to keep the folder stucture, replace it with e if you want only the files
'-aoa Overwrite All existing files without prompt.
'-aos Skip extracting of existing files.
'-aou aUto rename extracting file (for example, name.txt will be renamed to name_1.txt).
'-aot auto rename existing file (for example, name.txt will be renamed to name_1.txt).
'Use -r if you also want to unzip the subfolders from the zip file
'You can add -ppassword if you want to unzip a zip file with password (only 7zip files)
'Change "*.*" to for example "*.txt" if you only want to unzip the txt files
'Use "*.xl*" for all Excel files: xls, xlsx, xlsm, xlsb
ShellStr = PathZipProgram & "7zFM.exe x -aoa -r" _
& " " & Chr(34) & FileNameZip & Chr(34) _
& " -o" & Chr(34) & NameUnZipFolder & Chr(34) & " " & "*.*"
ShellAndWait ShellStr, vbHide
MsgBox "Look in " & NameUnZipFolder & " for extracted files"
End If
End Sub
Where it says to include the "-ppassword" failed to do, it does not return. I tried to do it with a file without password, in this same code, and it doesn’t extract, I just didn’t understand why. I put the . exe from 7zip according to my directory, only when it arrives at this extract step, it stops and brings no return. In the case below, "15100086230090" is the password, so it would be -p15100086230090 correct?
ShellStr = PathZipProgram & "7zFM.exe x -aoa -r -p15100086230090" _
& " " & Chr(34) & FileNameZip & Chr(34) _
& " -o" & Chr(34) & NameUnZipFolder & Chr(34) & " " & "*.*"