Link to home
Start Free TrialLog in
Avatar of Apu_Shah
Apu_Shah

asked on

VBA or VBS to Zip files in a folder - Win XP , Using Access (VBA Code)

I am using Microsoft access on windows XP.

Here is what i want.

VBA code or VBS to

Zip files in a local folder.

Assumptions: Files and folders located locally.
                    Zip using winzip.
                    Zip a file from the folder and place the zipped version of the file in the same location with different name.

Let me know, if you have answer to this.

Thanz
SOLUTION
Avatar of MilanKM
MilanKM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Apu_Shah
Apu_Shah

ASKER

I get error at  DefPath = Application.DefaultFilePath

 Application.DefaultFilePath

I think i am missing some Lib.

Let me know,

Thanz
have u chacked PathWinZip = "C:\program files\winzip\" or anything else

:)

Yes, its a valid path.

.DefaultFilePath

Method or data member not found
just checked it out. Works fine. Trying to find out the prob.
I am using MS ACCESS 2002.

I am sure i am missing some lib , in reference.
Avatar of amit_g
That portion of the code is only creating a filepath. That you can build it yourself. Remove

    ' Build the path and name for the zip file
    DefPath = Application.DefaultFilePath
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If

    FileNameZip = DefPath & strDate & ".zip"

and just have

    FileNameZip = "C:\WhateverDir\WhateverSubDir\WhateverFile.zip"


Sub Zip_Folder_And_SubFolders()
    Dim PathWinZip As String, FileNameZip As String, FolderName As String
    Dim ShellStr As String, strDate As String, DefPath As String
 
    PathWinZip = "C:\program files\winzip\"
    'This will check if this is the path where WinZip is installed.
    If Dir(PathWinZip & "winzip32.exe") = "" Then
        MsgBox "Please find your copy of winzip32.exe and try again"
        Exit Sub
    End If
 
 
    FileNameZip = "C:\Documents and Settings\user\Desktop\Desktop\abc\test.zip"
 
    'Fill in the folder name
    FolderName = "C:\Documents and Settings\user\Desktop\Desktop\abc\"
 
    'Add a slash at the end if the user forget it
    If Right(FolderName, 1) <> "\" Then
        FolderName = FolderName & "\"
    End If
 
     'Zip the folder, -r is Include subfolders, -p is folder information
    ShellStr = PathWinZip & "Winzip32 -min -a -r -p" _
               & " " & Chr(34) & FileNameZip & Chr(34) _
               & " " & Chr(34) & FolderName & Chr(34)
   gives error --> ShellAndWait ShellStr, vbHide
 
    MsgBox "The zipfile is ready in: " & FileNameZip
End Sub
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Sub Zip_Folder_And_SubFolders()
    Dim PathWinZip As String, FileNameZip As String, FolderName As String
    Dim ShellStr As String, strDate As String, DefPath As String
 
    PathWinZip = "C:\program files\winzip\"
    'This will check if this is the path where WinZip is installed.
    If Dir(PathWinZip & "winzip32.exe") = "" Then
        MsgBox "Please find your copy of winzip32.exe and try again"
        Exit Sub
    End If
 
 
    FileNameZip = "C:\Documents and Settings\abc\test.zip"
 
    'Fill in the folder name
    FolderName = "C:\Documents and Settings\abc\"
 
    'Add a slash at the end if the user forget it
    If Right(FolderName, 1) <> "\" Then
        FolderName = FolderName & "\"
    End If
 
     'Zip the folder, -r is Include subfolders, -p is folder information
    ShellStr = PathWinZip & "Winzip32 -min -a -r -p" _
               & " " & Chr(34) & FileNameZip & Chr(34) _
               & " " & Chr(34) & FolderName & Chr(34)
    ShellAndWait ShellStr, vbHide
 
    MsgBox "The zipfile is ready in: " & FileNameZip
End Sub

Public Sub ShellAndWait(PathName As String)

Dim TaskID As Double

'Run application and return Process ID (TaskID)
TaskID = Shell(PathName, vbHide) ', vbNormalFocus)

While TaskExists(TaskID)
'Wait for TaskExists False
Wend

End Sub


Gives error : wrong number of arguments or invalid property.

Help...
You did not change

ShellAndWait ShellStr, vbHide

to

ShellAndWait ShellStr
sub or function not defined.

TaskExists
Add ...

Public Function TaskExists(TaskID As Double) As Boolean

'Raise Error if TaskID does not exist
On Error GoTo ErrorHandler

'Attempt to activate TaskID
AppActivate (TaskID)

'If AppActivate returns an error, we know that the
'process is finished. We handle the error and return
'the function as false--this allows the calling
'procedure to exit the While loop.

ErrorHandler: If Err.Number = 5 Then
TaskExists = False
Exit Function
Else
TaskExists = True
End If

End Function

http://experts.about.com/q/Using-MS-Access-1440/Link-tables-Schema-ini.htm
Thank you , it worked.