Zipping the current Excel Spreadsheet with PkZip

G

Guest

Is there anyway to zip the current spreadsheet using the DOS version of
PkZip? I don't have WinZip, but I do have PkZip. I just want to save the
worksheet with it's current name.

Thanks!
 
G

Guest

I got a compile error at the NewZip (FileNameZip) line. Unexpected Sub,
Function, Property. What did I do wrong?


Dim strDate As String, DefPath As String, sFName As String
Dim oApp As Object, iCtr As Long
Dim FName, vArr, FileNameZip

DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

strDate = Format(Now, " dd-mmm-yy h-mm-ss")
FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip"

'Browse to the file(s), use the Ctrl key to select more files
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls),
*.xls", _
MultiSelect:=True, Title:="Select
the files you want to zip")

If IsArray(FName) = False Then
'do nothing
Else
'Create empty Zip File
NewZip (FileNameZip) 'Here is where the error pccurs.

Set oApp = CreateObject("Shell.Application")
 
R

Ron de Bruin

Thanks for the feedback

I will add a example to zip the activeworkbook and mail it also this weekend
 
G

Guest

That would complete my project! Thanks again!

Ron de Bruin said:
Thanks for the feedback

I will add a example to zip the activeworkbook and mail it also this weekend
 
G

Guest

Well...I'm trying one more thing to completely automate the process. I have
created a 2nd workbook that is called from the 1st workbook that I ultimately
want to zip. I write the variables I need to sheet1 and then reload those
variables when the zip macro is run. I've modified your code to use the path
and zip file name I want, but the one thing I can't make it do is
automatically use the filename (SName) when it gets to the part where you
open the file to zip. It looks like the code won't support inserting the
filename like the SaveAsFile will. Could you look at this and let me know?
Here is the code in the zip.xls worksheet:

Dim strDate As String, DefPath As String, sFName As String
Dim oApp As Object, iCtr As Long
Dim FName, vArr, FileNameZip
Dim ZName As String
Dim SName As String
Dim File_path As String

Worksheets(1).Select
ZName = Range("A1").Value 'contains the zip file name I want
SName = Range("A2").Value 'contains the file name I want to zip
File_path = Range("A3").Value 'contains the path of the both the file I
want to zip and the target of the zip file I want to create.

'Original Code Goes to My Documents
'DefPath = Application.DefaultFilePath

'Sets path to where my files are located
DefPath = File_path

If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

'Original Code to Set the Zip File Name
'strDate = Format(Now, " dd-mmm-yy h-mm-ss")
'FileNameZip = File_path & "MyFilesZip " & strDate & ".zip"
'I want to use my own zip name
FileNameZip = DefPath & ZName

'Browse to the file(s), use the Ctrl key to select more files
'Original Code to set which file to zip
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls),
*.xls", _
MultiSelect:=True, Title:="Select
the files you want to zip")

(THIS IS WHERE I'M TRYING TO USE THE SNAME)
'I want automatically use my own filename
FName = Application.GetOpenFilename(SName)

If IsArray(FName) = False Then
'do nothing
Else
'Create empty Zip File
NewZip (FileNameZip)

Set oApp = CreateObject("Shell.Application")

For iCtr = LBound(FName) To UBound(FName)
vArr = Split97(FName(iCtr), "\")
sFName = vArr(UBound(vArr))
If bIsBookOpen(sFName) Then
MsgBox "You can't zip a file that is open!" & vbLf & _
"Please close: " & FName(iCtr)
Else
'Copy the file to the compressed folder
oApp.Namespace(FileNameZip).CopyHere (FName(iCtr))
End If
Next iCtr

MsgBox "You will find your zipfile here: " & FileNameZip
Set oApp = Nothing
End If
End Sub
Sub NewZip(sPath)
'Create empty Zip File
'keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub

Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function

Function Split97(sStr As Variant, sdelim As String) As Variant
'Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function
 
R

Ron de Bruin

Hi David

I must go to a party this evening.
I update the site tomorrow and look at your problem
 
M

Myles

I have newly installed Microsoft Office. Now, launching a file, sa
Personal.xls (containing macros) from Excel startup folder, trigger
off the error message: MODULE NOT FOUND. The file consequently neve
gets open. Can someone familiar with this problem help me with
solution?

Thanks

Myle
 
G

Guest

I used the first part of the file you suggested and it's working fine. Was
hoping to delete the existing file...only keeping the zip file, but just
can't be done from within the workbook. Thanks!
 
R

Ron de Bruin

Hi David

Ok, Try this


Sub Zip_ActiveWorkbook_And_Delete_ActiveWorkbook()
Dim strDate As String, DefPath As String, strbody As String
Dim oApp As Object
Dim FileNameZip, FileNameXls

DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

'Create date/time string and the temporary xls file and zip file name
strDate = Format(Now, " dd-mmm-yy h-mm-ss")
FileNameZip = DefPath & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & strDate & ".zip"
FileNameXls = DefPath & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & strDate & ".xls"

If Dir(FileNameZip) = "" And Dir(FileNameXls) = "" Then

'Make copy of the activeworkbook
ActiveWorkbook.SaveCopyAs FileNameXls

'Create empty Zip File
NewZip (FileNameZip)

'Copy the file in the compressed folder
Set oApp = CreateObject("Shell.Application")
oApp.NameSpace(FileNameZip).CopyHere FileNameXls

'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.NameSpace(FileNameZip).items.Count = 1
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0

Set oApp = Nothing

'Delete the temporary xls file
Kill FileNameXls

MsgBox "You find the zipfile here: " & FileNameZip

'Delete the activeworkbook
With ActiveWorkbook
.ChangeFileAccess xlReadOnly
Kill .FullName
.Close False
End With

Else
MsgBox "FileNameZip or/and FileNameXls exist"
End If
End Sub

Sub NewZip(sPath)
'Create empty Zip File
'keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub
 
G

Guest

I'm no expert, but instead of putting it in the personal.xls, but it in the
ThisWorkbook module. Alt 11 to start the marco editor, CTRL+R to see the
Project Viewer and look for the last module under your workbook.
You didn't include any code, not did you post your question in the right
place. Try again if this doesn't help.
 
G

Guest

Hi Ron.

I was trying to modify the code below (to compress a file & email it -- the
zipped file -- yet keep it and the original file in the same folder) and it
would stop and give me the following error:

Run-time error ‘91’: Object variable or With block variable not set.

The line

Set oApp = CreateObject("Shell.Application") 'SETS oApp to nothing

goes from oApp = Nothing to not even showing any value at all after it is
executed (hovering over oApp), and

oApp.Namespace(FileNameZip).CopyHere FileNameXls

give me the error message. Can you, or anyone, help me understand why it
would do that?

Thx,
Dante Encinas
(e-mail address removed)
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top