Monday, July 4, 2011

Function to create zip file of folder

Function ZipAttachment

    ZipAttachment = ""

    ' Declare the required variables
    Dim objZip, objSA, objFolder, zipFile, FolderToZip
   
    zipFile = Environment.Value("c:\ResultFolder") & ".zip"
    FolderToZip = Environment.Value("c:\ResultFolder")

    'Create the basis of a zip file.
    CreateObject("Scripting.FileSystemObject") _
    .CreateTextFile(zipFile, True) _
    .Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

    ' Create the object of the Shell
    Set objSA = CreateObject("Shell.Application")
   
    ' Add the folder to the Zip
    Set objZip = objSA.NameSpace(zipFile)
    Set objFolder = objSA.NameSpace(FolderToZip)
    objZip.CopyHere(objFolder.Items)
   
    ' Wait for the folder to be added in the zip
    ' Increase the Wait time is the folder is too large
    WScript.Sleep 2000

    ZipAttachment = zipFile

End function

No comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | JCpenney Printable Coupons