1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
Option Explicit
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) _
As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STILL_ACTIVE = &H103
Public UZipDownload As Boolean
Private Sub ShellAndWait(ByVal PathName As String, Optional WindowState)
Dim hProg As Long
Dim hProcess As Long, ExitCode As Long
UZipDownload = True
If IsMissing(WindowState) Then WindowState = 1
'hProg est un "process ID under Win32"
hProg = Shell(PathName, WindowState)
'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
If ExitCode <> 0 Then
UZipDownload = False
Select Case ExitCode
Case 1
MsgBox "Erreur 7z.exe code " & ExitCode & ": Warning (Non fatal error(s))"
Case 2
MsgBox "Erreur 7z.exe code " & ExitCode & ": Fatal Error"
Case 7
MsgBox "Erreur 7z.exe code " & ExitCode & ": Command line error"
Case 8
MsgBox "Erreur 7z.exe code " & ExitCode & ": Not enough memory for operation"
Case 255
MsgBox "Erreur 7z.exe code " & ExitCode & ": User stopped the process"
End Select
End If
hProg = CloseHandle(hProcess)
End Sub
'With this example you unzip a fixed zip file: ZipLongName = "C:\Users\Ron\Test.zip"
'Note this file must exist, this is the only thing that you must change before you test it
'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:
'DestinationPath = "C:\Users\Ron\TestFolder\"
'Read the comments in the code about the commands/Switches in the ShellStr
Sub UnzipFile_7z(ZipLongName As String, DestinationPath As String, ProgramName As String, ByVal ProgramPath As String)
Dim ShellStr As String
If Right(ProgramPath, 1) <> "\" Then ProgramPath = ProgramPath & "\"
If Dir(ProgramPath & ProgramName) = "" Then
MsgBox ProgramName & " not found in " & ProgramPath
Exit Sub
End If
'extrait (e) toute (*.*)l'archive, renomme l'existant(-aot)
ShellStr = ProgramPath & ProgramName & " e -aot" & " " & Chr(34) & ZipLongName & _
Chr(34) & " -o" & Chr(34) & DestinationPath & Chr(34) & " " & "*.*"
Call ShellAndWait(ShellStr, 1)
If UZipDownload = False Then
MsgBox "Unzip Failure"
End If
End Sub |
Partager