Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

zip access vba

Status
Not open for further replies.

JoanaSantos

Programmer
Feb 24, 2015
33
EU
Hi,

I want to zip some files in my folder.
i used this code:

'Compacta e Zip
Private Declare Sub Sleep Lib "kernel32" ( _
ByVal dwMilliseconds As Long _
)


Sub compactar_BD01()
Application.SetOption "Auto compact", True

'nome do zip / nome do ficheiro origem

Zip "G:\OrcControlo\SCC\Processo\Backups\00Custos_tabelas_gerais.zip", "G:\OrcControlo\SCC\Processo\00Custos_tabelas_gerais.mdb"
Zip "G:\OrcControlo\SCC\Processo\Backups\01Custos_SAN.zip", "G:\OrcControlo\SCC\Processo\01Custos_SAN.accdb"
Zip "G:\OrcControlo\SCC\Processo\Backups\03Custos_FH2.zip", "G:\OrcControlo\SCC\Processo\03Custos_FH2.mdb"
Zip "G:\OrcControlo\SCC\Processo\Backups\04Custos_FHC.zip", "G:\OrcControlo\SCC\Processo\04Custos_FHC.mdb"
Zip "G:\OrcControlo\SCC\Processo\Backups\05Custos_FHD.zip", "G:\OrcControlo\SCC\Processo\05Custos_FHD.mdb"
Zip "G:\OrcControlo\SCC\Processo\Backups\06Custos_FHP.zip", "G:\OrcControlo\SCC\Processo\06Custos_FHP.mdb"

End Sub

Public Sub Zip( _
ZipFile As String, _
InputFile As String _
)
On Error GoTo ErrHandler
Dim FSO As Object 'Scripting.FileSystemObject
Dim oApp As Object 'Shell32.Shell
Dim oFld As Object 'Shell32.Folder
Dim oShl As Object 'WScript.Shell
Dim i As Long
Dim l As Long

Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FileExists(ZipFile) Then
'Create empty ZIP file
FSO.CreateTextFile(ZipFile, True).Write _
"PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
End If

Set oApp = CreateObject("Shell.Application")
Set oFld = oApp.Namespace(CVar(ZipFile))
i = oFld.items.Count
oFld.CopyHere (InputFile)

Set oShl = CreateObject("WScript.Shell")

'Search for a Compressing dialog
Do While oShl.AppActivate("Compressing...") = False
If oFld.items.Count > i Then
'There's a file in the zip file now, but
'compressing may not be done just yet
Exit Do
End If
If l > 30 Then
'3 seconds has elapsed and no Compressing dialog
'The zip may have completed too quickly so exiting
Exit Do
End If
DoEvents
Sleep 100
l = l + 1
Loop

' Wait for compression to complete before exiting
Do While oShl.AppActivate("Compressing...") = True
DoEvents
Sleep 100
Loop

ExitProc:
On Error Resume Next
Set FSO = Nothing
Set oFld = Nothing
Set oApp = Nothing
Set oShl = Nothing
Exit Sub
ErrHandler:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & _
": " & Err.Description, _
vbCritical, "Unexpected error"
End Select
Resume ExitProc
Resume
End Sub


i don't want zip file by file. i want to zip all the files at the same time. i want to have a zip and when i run the zip i should be able to see the 6 files that i want..

can you help ?
 
I can't answer the question. As I understand it, Microsoft do not support programmatic zipping and unzipping (and patent issues may have some bearing on the issue). I have had mixed success in the past, and generally consider anything that even partially works to be a bonus [smile]. Although I can't lay my hands on it at the moment, I have played around with code to do the compression directly but it's a lot of bit-twiddling and VBA is not the right language for it. There may be third party solutions that can be used - I'm not sure - but most, I suspect, will not work easily with VBA.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
>Microsoft do not support programmatic zipping and unzipping

Sure it does. It just doesn't make it easy for VBA programmers. The first method - as per the basic example (and links) above uses the Shell and it's ability to handle simple zip files. A .NETs system.io.compression assembly handles zip fairly well, particularly the 4.5 libraries.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top