Vba Zip File With Password: Excel
sevenZipExe = "C:\Program Files\7-Zip\7z.exe"
' Command: a (add), -tzip, -r (recurse), -p, -mx=9 cmd = """" & sevenZipExe & """ a -tzip """ & outputZip & """ """ & _ folderPath & """ -r -p" & pwd & " -mx=9 -y"
Sub ZipWithPassword_WinRAR() Dim rarPath As String Dim source As String Dim target As String Dim pwd As String rarPath = "C:\Program Files\WinRAR\rar.exe" source = "C:\Temp\DataFolder" target = "C:\Temp\Secure.rar" ' WinRAR creates .rar, not .zip pwd = "Secret123"
Dim wsh As Object Set wsh = CreateObject("WScript.Shell") wsh.Run cmd, 0, True excel vba zip file with password
CreateObject("Shell.Application").Namespace(ZipPath).CopyHere FilePath This method does not support passwords . You’ll get an unprotected ZIP every time. So we need an alternative. Method 1: Using Command‑Line 7‑Zip (Most Reliable) 7‑Zip is a free, powerful archiver. Its command‑line version 7z.exe supports AES‑256 encryption with passwords. Step 1: Install 7‑Zip Download and install 7‑Zip. The default path is C:\Program Files\7-Zip\7z.exe . Step 2: VBA Code Sub ZipWithPassword_7Zip() Dim FileToZip As String Dim ZipFileName As String Dim Password As String Dim SevenZipPath As String Dim Cmd As String ' --- Configuration --- FileToZip = "C:\Temp\Confidential.xlsx" ' File or folder to zip ZipFileName = "C:\Temp\Confidential.zip" Password = "MyStrongP@ssw0rd" SevenZipPath = "C:\Program Files\7-Zip\7z.exe"
Dim wsh As Object Set wsh = CreateObject("WScript.Shell") wsh.Run cmd, 0, True
In this post, I’ll walk you through three reliable methods to create password‑protected ZIP files directly from Excel VBA. The standard VBA approach for zipping uses Windows Shell: sevenZipExe = "C:\Program Files\7-Zip\7z
Published on: April 17, 2026 | Category: Excel Automation
' --- Build command --- ' a = add, -tzip = zip format, -p = password, -mx=9 = max compression Cmd = """" & SevenZipPath & """ a -tzip """ & ZipFileName & """ """ & _ FileToZip & """ -p" & Password & " -mx=9 -y"
' --- Run command (hidden) --- Dim wsh As Object Set wsh = CreateObject("WScript.Shell") wsh.Run Cmd, 0, True ' 0 = hidden, True = wait until finished The default path is C:\Program Files\7-Zip\7z
MsgBox "Protected RAR archive created." End Sub ⚠️ Note: This creates a .rar file, not .zip . For strict ZIP compatibility, stick with 7‑Zip. If you cannot install 7‑Zip but use Windows 10/11, PowerShell’s System.IO.Compression can create ZIPs, but it does NOT support passwords natively . However, you can combine it with .NET’s DotNetZip or SharpCompress – but that requires additional DLLs.
Sub BatchZipWithPassword() Dim folderPath As String Dim outputZip As String Dim pwd As String Dim sevenZipExe As String Dim cmd As String folderPath = "C:\Reports\2026-04\" ' Folder to compress outputZip = "C:\Archives\Reports_April.zip" pwd = InputBox("Enter ZIP password:", "Security") If pwd = "" Then Exit Sub
– Your Excel Automation Expert
' Command: a = add, -p = password, -ep1 = no full paths Dim cmd As String cmd = """" & rarPath & """ a -p" & pwd & " -ep1 """ & target & """ """ & source & """"
