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:

Some videos you may like

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Rolly_Sefu

Board Regular
Joined
Oct 25, 2013
Messages
149
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,001
Messages
5,622,125
Members
415,878
Latest member
jjj12345

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
Top