Results 1 to 10 of 10

Thread: [RESOLVED] Vista: help on converting code to late binding

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Resolved [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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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).

  3. #3
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Vista: help on converting code to late binding


  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    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.

  7. #7

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Vista: help on converting code to late binding

    Quote Originally Posted by si_the_geek View Post
    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    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)

  9. #9

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Vista: help on converting code to late binding

    Yes, I will use variant if it will work.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  10. #10

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Vista: help on converting code to late binding

    Yes, using Variants works. Thanks Magic Ink.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width