Have VBA to set password to all xls files in one folder

scorpio3st

New Member
Joined
Sep 7, 2021
Messages
22
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I need help with one macro:

Sub ProtectAll()
Dim xWorkBooks As Workbook
Dim xExitFile As String
Dim xPassWord As Variant
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder [Kutools for Excel]"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then
Exit Sub
Else
xStrPath = xStrPath + "\"
End If
xPassWord = Application.InputBox("Enter password", "Kutools for Excel", , , , , , 2)
If (xPassWord = False) Or (xPassWord = "") Then
MsgBox "Password cannot be blank!", vbInformation, "Kutools for Excel"
Exit Sub
End If

xExitFile = Dir(xStrPath & xFile)
On Error Resume Next
Application.ScreenUpdating = False
Do While xExitFile <> ""
Set xWorkBooks = Workbooks.Open(xStrPath & xExitFile)
Application.DisplayAlerts = False
xWorkBooks.SaveAs Filename:=xWorkBooks.FullName, Password:=xPassWord
Application.DisplayAlerts = True
Workbooks(xExitFile).Close False
Set xWorkBooks = Nothing
xExitFile = Dir
Loop
Application.ScreenUpdating = True
MsgBox "Successfully protect!", vbInformation, "Kutools for Excel"
End Sub

Now this Macro open every xls document and add password. Is it possible to add password without opening?
Thanks in advance.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,214,965
Messages
6,122,496
Members
449,089
Latest member
Raviguru

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