How can I compress a file using VB-code in MS Access

Posted on 2011-10-20
Last Modified: 2013-12-20
I have a file that is generated in my MS Access db. I want to be able to send the same file by email (also using VB-code); however, Outlook doesn't like the file format, so the file needs to be compressed to *.zip before sending it. The file is accessible from the same network drive as the db itself.

Can the compression be done using VB-code in MS Access?

Code samples deeply appreciated :)
Question by:PerMagnusStrom
    LVL 28

    Accepted Solution

    Here's sample code to add files to a zip archive.  If Outlook doesn't like the file type now I believe it still won't like it even when compressed in a zip archive.  You may need to change the file extension.
    OM Gang

    Public Function AddFilesToZip()
    On Error GoTo Err_AddFilesToZip

        Dim objFSO As Object, objZip As Object, objShell As Object
        Dim objFolder As Object, objFile As Object
        Dim sngStart As Single
        Dim strPath As String, strZip As String
        strPath = "C:\test\"   'Path to read files from
        strZip = "C:\test2\"  'Output zip file
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objZip = objFSO.CreateTextFile(strZip)
        objZip.WriteLine Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objFSO.GetFolder(strPath)
            'loop through files - adding them to the zip
        For Each objFile In objFolder.Files
            objShell.NameSpace("" & strZip).CopyHere objFile.PATH
            sngStart = Timer
            Do While Timer < sngStart + 2


            'destroy object variables
        Set objFile = Nothing
        Set objFolder = Nothing
        Set objShell = Nothing
        Set objZip = Nothing
        Set objFSO = Nothing
        Exit Function

        MsgBox Err.Number & ", " & Err.Description, , "Error in Function AddFilesToZip of Module Module7"
        Resume Exit_AddFilesToZip
    End Function

    Author Closing Comment

    Simply outstanding :)

    Thank you so much!

     - PM

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now