|
-
May 5th, 2009, 04:01 AM
#1
Thread Starter
Fanatic Member
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 ** 
-
May 5th, 2009, 03:10 PM
#2
Re: VBA Code to Winzip external File
Thread moved from "Database Development" forum to "Office Development/VBA" forum
-
May 5th, 2009, 04:52 PM
#3
Re: VBA Code to Winzip external File
the shell object seems to do this quite well
vb Code:
p = "c:\test" z = p & "\mytest.zip" Open z For Output As 1 Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0) Close 1 Set sh = CreateObject("shell.application") sh.namespace(z).copyhere p 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
-
May 6th, 2009, 03:30 AM
#4
Thread Starter
Fanatic Member
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 ** 
-
May 6th, 2009, 04:29 AM
#5
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
-
May 6th, 2009, 06:58 AM
#6
Thread Starter
Fanatic Member
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 ** 
-
May 6th, 2009, 04:32 PM
#7
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:
z = left(filename, instr(filename, ".")) & "zip" 'truncate filename at . and add zip ff = FreeFile() Open Z For Output As #ff Print #ff, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0) ' makes zip file Close #ff set MyAppID = CreateObject("shell.application") 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
-
May 7th, 2009, 02:43 AM
#8
Thread Starter
Fanatic Member
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 ** 
-
May 7th, 2009, 04:43 AM
#9
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
-
May 7th, 2009, 05:53 AM
#10
Thread Starter
Fanatic Member
-
May 7th, 2009, 06:46 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|