This is the code I have which works in Vista with early binding. It is also somewhat imperfect since I used CreateObject to instantiate the zipObject, I don't know its end type.
Code:'this will work in Vista with early binding, reference is Microsoft SHell Controls and Automation Public Sub ZipFile(ByVal strFileToZip As String, ByVal strTargetZip As String, Optional ByVal bolFileType As Boolean = True) CreateEmptyZip strTargetZip Dim folder As folder Dim zipObject As IShellDispatch5 'don't know what type to instantiate it to so I used CreateObject Set zipObject = CreateObject("Shell.Application") Set folder = zipObject.NameSpace(strTargetZip) If bolFileType Then folder.CopyHere strFileToZip Else folder.CopyHere zipObject.NameSpace(strFileToZip).Items End If End Sub
And this is my attempt to make it late bind but I am not successful, folder is always nothing and I am not sure what to do with it to be able to instantiate it. Any guidance will be appreciated.
Code:Public Sub ZipFile(ByVal strFileToZip As String, ByVal strTargetZip As String, Optional ByVal bolFileType As Boolean = True) CreateEmptyZip strTargetZip Dim folder As Object Dim zipObject As Object Set zipObject = CreateObject("Shell.Application") Set folder = zipObject.NameSpace(strTargetZip) If Not folder Is Nothing Then If bolFileType Then folder.CopyHere strFileToZip Else folder.CopyHere zipObject.NameSpace(strFileToZip).Items End If Else MsgBox "folder is nothing!" End If End Sub Private Sub CreateEmptyZip(ByVal sPath As String) Dim strZIPHeader As String strZIPHeader = Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String$(18, 0) ' header required to convince Windows shell that this is really a zip file CreateObject("Scripting.FileSystemObject").CreateTextFile(sPath).Write strZIPHeader End Sub




Reply With Quote