Results 1 to 11 of 11

Thread: Resolved - VBA Code to Winzip external File

  1. #1

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721

    Resolved - VBA Code to Winzip external File

    Hi

    Does anyone have or possibly point me in the right direction of some VBA
    Code that will winzip a file without manual intervention?


    Thanks
    Last edited by holly; May 8th, 2009 at 06:41 AM.
    ** HOLLY **

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

    Re: VBA Code to Winzip external File

    Thread moved from "Database Development" forum to "Office Development/VBA" forum

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

    Re: VBA Code to Winzip external File

    the shell object seems to do this quite well
    vb Code:
    1. p = "c:\test"
    2.       z = p & "\mytest.zip"
    3.       Open z For Output As 1
    4.           Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
    5.       Close 1
    6.       Set sh = CreateObject("shell.application")
    7.       sh.namespace(z).copyhere p
    8.       Set sh = Nothing
    copies all the files in folder test into new zip file in same folder
    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

  4. #4

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721

    Re: VBA Code to Winzip external File

    Hi

    I have copied your code as below and am still having problems

    HTML Code:
        Dim P As String
        Dim Z As String
        Dim ZipFileName As String
        Dim MyAppID As String
        
        P = filename
        
        If Not Right(P, 4) = ".zip" Then
            P = P & ".zip"
        Else
            P = P & ".CSV"
        End If
        
        Z = P
        
        Open Z For Output As 1
            Print #1, filename
        Close Z
        
       ' MyAppID = Shell("C:\Program Files\WinZip\Winzip.exe", 1)   ' Run
      '  AppActivate MyAppID ' Activate Microsoft
    
        MyAppID = CreateObject("shell.application")
    [COLOR="Red"]    MyAppID = NameSpace(Z).copyhere & P[/COLOR]
    
        
        Set MyAppID = Nothing
    
    End Sub
    I get an error on this line as it states 'Invalid qualifier'

    Pls help!!
    ** HOLLY **

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

    Re: VBA Code to Winzip external File

    i can see that you would, is filename a valid string in the scope of this proceedure?
    what would the content of filename be when testing?

    opening a zip file for output would overwrite it as a text file, even though it has zip extention

    no & required in that line
    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

  6. #6

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721

    Re: VBA Code to Winzip external File

    This is the full procdure whereby I'm passing in the files I wish to zip!


    Code:
    Public Sub CreateZipFile(filename As String)
        Dim P As String
        Dim Z As String
        Dim ZipFileName As String
        Dim MyAppID As FileSystemObject
        Dim ff As Integer
        
        Set MyAppID = New FileSystemObject
        
        P = filename
        
        If Not Right(P, 4) = ".zip" Then
            P = Right(P, 4) & ".zip"
        Else
            P = P & ".CSV"
        End If
        
        Z = P
        
        ff = FreeFile()
       
        Open Z For Output As #ff
            Print #ff, filename & ","
        Close #ff
            
        MyAppID = CreateObject("shell.application")
      '  MyAppID.NameSpace(Z).copyhere P
        
         Set MyAppID = Nothing
    
    End Sub
    I have numerous problems whereby the file is not being zipped?
    b) what reference's wouldI need ?

    Thanks
    ** HOLLY **

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

    Re: VBA Code to Winzip external File

    if you pass an existing zipfile to the proceedure, opening it for output then writing text(filename) to it, will convert it to txt file, with .zip
    my example created a new zip file by writing the correct bytes to it

    in your current code if filename is not zip file you should just exit sub as the code will not do anything for csv or other file type
    if you pass a zipfile your else will add.csv to it, so you could have somefile.zip.csv or somefile.***.zip, depending on what is in file name
    possibly you want to pass a csv to the sub and zip it to file with same name
    as filename is a variable already there is no need for p
    vb Code:
    1. z = left(filename, instr(filename, ".")) & "zip"    'truncate filename at . and add zip
    2.     ff = FreeFile()
    3.     Open Z For Output As #ff
    4.         Print #ff, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)   ' makes zip file
    5.     Close #ff
    6.     set MyAppID = CreateObject("shell.application")
    7.     MyAppID.NameSpace(Z).copyhere filename    'add the original file to the created zip


    declaring and setting myappid as a filesystemobject is incorrect, if you want to add a reference, which is not required, it should be, i believe, to shell32, otherwise just declare myappid as object
    and it has to be
    set myappid = createobject("shell.application")

    please reread the original example i posted
    Last edited by westconn1; May 6th, 2009 at 04:43 PM.
    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

  8. #8

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721

    Re: VBA Code to Winzip external File

    I'm sorry to be a pain but i'm getting an error on this line

    Code:
        Set MyAppID = CreateObject("shell.application")
        MyAppID.NameSpace(Z).copyhere filename
    stating ' Object variable not set' but as you can see it is with the line above

    Thanks for your help and patience!!!
    ** HOLLY **

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

    Re: VBA Code to Winzip external File

    is z a fully qualified filename to a folder or valid zip file, if the zip file is not valid it will not appear as a folder to the shell

    the original code was copied from a working example

    make sure the the fiel specified in Z is a working zip file, even if you created it here, i should be able to be opened in winzip and files added, or files added in windows explorer

    have you changed the dimension for myappid from filesystemobject
    check in the locals window if myappid is shell application or some other object
    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

  10. #10

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721

    Re: VBA Code to Winzip external File

    Thanks very much....it now works a treat!!

    Just a quick question...if I wanted to encrypt the file or place a pwd on the
    *.csv file, how easy would it be?
    ** HOLLY **

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

    Re: VBA Code to Winzip external File

    you can set a password on zip files, but i don't know how to do in code
    there are many examples on encrypting strings, to differing strengths, in the vb6 forum, most of which would work in VBA, csv files are just a string (text), so you could encrypt the content of the file before zipping
    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

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