Mã:
Option Compare Database
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
#Else
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
#End If
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STILL_ACTIVE = &H103
Private strWinRarPath As String
Private strErrLogFile As String
Public Function 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 Function
Public Function WinRarIt(strSourceFile As String, strFormat As String, Optional strMethod As String, _
Optional strPassword As String, Optional ByRef strMSG As String) As Boolean
On Error GoTo Err_handler
Dim strFileNameRar As String
Dim strCMDLine As String
WinRarIt = False
If CheckWinRar = False Then Exit Function
strFileNameRar = Left$(strSourceFile, InStrRev(strSourceFile, ".")) & strFormat
strCMDLine = Chr(34) & strWinRarPath & Chr(34) & " m -ep1 -ibck " '-inul
If strMethod <> "" Then strCMDLine = strCMDLine & strMethod
If strPassword <> "" Then strCMDLine = strCMDLine & " -p" & strPassword
strCMDLine = strCMDLine & " -ilog" & strErrLogFile & " " & Chr(34) & strFileNameRar _
& Chr(34) & " " & Chr(34) & strSourceFile & Chr(34)
'Debug.Print strCMDLine
Call ShellAndWait(strCMDLine, vbHide)
strSourceFile = strFileNameRar
If Dir(strSourceFile, vbNormal) <> "" Then
WinRarIt = True
strMSG = strMSG & vbCrLf & "-Nen CSDL thanh cong"
Else
strMSG = strMSG & vbCrLf & "-Nen CSDL That bai"
End If
Err_Exit:
Exit Function
Err_handler:
If Err.Number <> 0 Then MsgBox Err.Description, vbExclamation, "WinRarIt Error: " & Err.Number
strMSG = strMSG & vbCrLf & "-Nen CSDL That bai "
Resume Err_Exit
End Function
Public Function UnRarIt(strWinRarFile As String, Optional strUnRarToFolder As String, _
Optional strPassword As String, Optional ByRef strMSG As String) As Boolean
On Error GoTo Err_handler
Dim strFileNameRar As String
Dim strCMDLine As String
UnRarIt = False
If CheckWinRar = False Then Exit Function
If strUnRarToFolder = "" Then
strUnRarToFolder = Left$(strWinRarFile, InStrRev(strWinRarFile, "\"))
End If
strCMDLine = Chr(34) & strWinRarPath & Chr(34) & " e -ibck "
If strPassword <> "" Then strCMDLine = strCMDLine & "-p" & strPassword
strCMDLine = strCMDLine & " -ilog" & strErrLogFile & " " & Chr(34) & strWinRarFile & Chr(34) _
& " " & Chr(34) & strUnRarToFolder & Chr(34)
'Debug.Print strCMDLine
Call ShellAndWait(strCMDLine, vbHide)
If Dir(strUnRarToFolder) <> "" Then
UnRarIt = True
strMSG = strMSG & vbCrLf & "-Giai nen thanh cong"
Else
strMSG = strMSG & vbCrLf & "-Giai nen that bai"
End If
Err_Exit:
Exit Function
Err_handler:
If Err.Number <> 0 Then MsgBox Err.Description, vbExclamation, "UnRarIt Error: " & Err.Number
strMSG = strMSG & vbCrLf & "-Giai nen that bai "
Resume Err_Exit
End Function
Function CheckWinRar() As Boolean
strWinRarPath = CurrentProject.path & "\Tools\WinRar\WinRARPortable.exe"
strErrLogFile = "ErrorLog.txt"
CheckWinRar = True
If Dir(strWinRarPath) = "" Then
MsgBox "Khong tim thay WinRAR theo duong dan: " & vbCrLf & strWinRarPath
CheckWinRar = False
End If
End Function