Saturday, April 27, 2013

Zip Files using VBA

VBA Code to zip files:

Private Sub ZipFile_FX(ZipFileName As String, fileToBeZipped As String)
Const ZIPEXELOCATION = "c:\program files\winzip\winzip32.exe"
Shell ZIPEXELOCATION & " -a " & Chr(34) & ZipFileName & Chr(34) & _
" " & Chr(34) & fileToBeZipped & Chr(34), vbNormalFocus
End Sub

sub zips()
Call ZipFile_FX("c:\", "c:\b.xls")
end sub

1 comment:

  1. Code to unzip a file:

    Sub TestRun()
    'Change this as per your requirement
    Call UnZip("C:\Zip Files", "C:\Zip Files\")
    End Sub
    Sub UnZip(strTargetPath As String, Fname As Variant)
    Dim oApp As Object
    Dim FileNameFolder As Variant
    If Right(strTargetPath, 1) <> Application.PathSeparator Then

    strTargetPath = strTargetPath & Application.PathSeparator
    End If
    FileNameFolder = strTargetPath
    Set oApp = CreateObject("Shell.Application")

    oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
    End Sub