|
-
Aug 26th, 2009, 01:55 AM
#1
[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
-
Aug 26th, 2009, 06:00 AM
#2
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).
-
Aug 26th, 2009, 07:08 AM
#3
Re: Vista: help on converting code to late binding
-
Aug 26th, 2009, 07:39 AM
#4
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 26th, 2009, 08:02 AM
#5
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")...
Last edited by LaVolpe; Aug 26th, 2009 at 08:06 AM.
-
Aug 26th, 2009, 09:36 AM
#6
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.
-
Aug 26th, 2009, 03:49 PM
#7
Re: Vista: help on converting code to late binding
 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.
 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.
 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.
-
Aug 26th, 2009, 04:32 PM
#8
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)
-
Aug 26th, 2009, 04:58 PM
#9
Re: Vista: help on converting code to late binding
Yes, I will use variant if it will work.
-
Aug 27th, 2009, 12:29 AM
#10
Re: Vista: help on converting code to late binding
Yes, using Variants works. Thanks Magic Ink.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|