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:\b.zip", "c:\b.xls")
end sub

1 comment:

  1. Code to unzip a file:

    Sub TestRun()
    Sheets("Unzip").Select
    'Change this as per your requirement
    Call UnZip("C:\Zip Files", "C:\Zip Files\Test.zip")
    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

    ReplyDelete