[RESOLVED] Vista: help on converting code to late binding
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
Re: Vista: help on converting code to late binding
Thread moved from Database forum to VB6 forum
One thing that seems likely to cause it is that you declare zipObject as IShellDispatch5, but use CreateObject("Shell.Application") to assign it.
Presumably that involves some kind of type conversion, which is presumably only possible with the Reference and data type (otherwise VB wont even know that conversion is needed).
Re: Vista: help on converting code to late binding
Re: Vista: help on converting code to late binding
is a valid zip file being created?
i went and tested in vista (home basic), got an error creating the zipfile
CreateObject("Scripting.FileSystemObject").CreateTextFile(sPath).Write strZIPHeader
works ok in xp
Re: Vista: help on converting code to late binding
IShellDispatch5 is Vista and above only. CoCreateInstance API may allow you to create an IShellDispatch5 instance if you can find the GUID for that interface. This method is a bit of a hack and not as easy as it sounds. A TLB would be much easier.
However, since you are only concerned with Vista, Magic Ink's link sounds interesting & notice the difference in that link
Your code:
Code:
Set zipObject = CreateObject("Shell.Application")
Set folder = zipObject.NameSpace(strTargetZip)
That other code, no Set command used:
Code:
With CreateObject("Shell.Application")...
Re: Vista: help on converting code to late binding
String literals work fine but if a variable is used as an argument to .NameSpace(arg) it should be a Variant; using a string variable gives me an error 91.
Re: Vista: help on converting code to late binding
Quote:
Originally Posted by
si_the_geek
Thread moved from Database forum to VB6 forum
One thing that seems likely to cause it is that you declare zipObject as IShellDispatch5, but use CreateObject("Shell.Application") to assign it.
Presumably that involves some kind of type conversion, which is presumably only possible with the Reference and data type (otherwise VB wont even know that conversion is needed).
Sorry for starting putting the thread in the wrong section, I may have mixed it up when I also started a thread in the database section.
Actually, zipObject is instantiated fine, I determined its type IShellDispatch5 by using typename so I have declared it as such. It was previously all latebinding using object and it worked in XP but not in Vista so I tried binding it early just to see if those methods still exists in Vista.
Quote:
Originally Posted by LaVolpe
IShellDispatch5 is Vista and above only. CoCreateInstance API may allow you to create an IShellDispatch5 instance if you can find the GUID for that interface. This method is a bit of a hack and not as easy as it sounds. A TLB would be much easier.
However, since you are only concerned with Vista, Magic Ink's link sounds interesting & notice the difference in that link
The code in Magic Ink's link is what I originally have and does work in XP but not in Vista.
Quote:
Originally Posted by Magic Ink
String literals work fine but if a variable is used as an argument to .NameSpace(arg) it should be a Variant; using a string variable gives me an error 91.
Thanks, will try that when I get to work.
Re: Vista: help on converting code to late binding
Your code appears to run fine for me on Vista (Ultimate fully SP'd) when I define the Sub;
Public Sub ZipFile(ByVal strFileToZip As Variant, ByVal strTargetZip As Variant, Optional ByVal bolFileType As Boolean = True)
Re: Vista: help on converting code to late binding
Yes, I will use variant if it will work.
Re: Vista: help on converting code to late binding
Yes, using Variants works. Thanks Magic Ink.