Generate and password protect zip file issue

Rolly_Sefu

Board Regular
Joined
Oct 25, 2013
Messages
149
Hello, i am generating a few pdf's from a worksheet, and i want to zip and password each file individually.
The code below works, but when i repeat the code multiple times it does not password protect all the zip files.
Any ideas why ? do i somehow need to reset zip parameters after each item ?

VBA Code:
Sub Test()
LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Sheets("sheet1").Range("A2:A" & LastRow)
For Each cell In Rng
    Sheets("Sheet2").Range("B2") = cell.Value
    Sheets("Sheet2").Range("B3") = cell.Offset(0, 1).Value
    Sheets("Sheet2").Range("B5") = cell.Offset(0, 2).Value
    UserName = Sheets("Sheet2").Range("B2") & " " & Sheets("Sheet2").Range("B3")
   
    Sheets("Sheet2").ExportAsFixedFormat Type:=xlTypePDF, _
    FileName:="C:\Users\Roland\Desktop\Zip test\" & UserName & ".pdf"
   
    strDestFileName = "C:\Users\Roland\Desktop\Zip test\" & UserName & ".zip"
    strSourceFileName = "C:\Users\Roland\Desktop\Zip test\" & UserName & ".pdf"
    str7ZipPath = "C:\Program Files\7-Zip\7z.exe"
    strPassword = cell.Offset(0, 3)
   
    strCommand = str7ZipPath & " -p" & strPassword & " a -tzip """ & strDestFileName & """ """ & strSourceFileName & """"
    Shell strCommand
    Application.Wait (Now + TimeValue("0:00:01"))
   
    Kill strSourceFileName
   
Next cell
End Sub

Also asked here Password protect a zipped file
Thank you
 
Last edited by a moderator:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I have noticed a strange thing, but maybe someone can help with the mystery.

Once i enter the password in the zip file, windows somehow remembers the password and does not ask for it a 2nd time.
i only noticed this after i changed the passwords then all files where asked for passwords again. ( but if i enter one passwords its somehow remembered )

is this a thing that happens when you call the shell function ? or a 7z zip thing ?

Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top