Vba- open/close password protected excel file

KUYJS

New Member
Joined
Sep 3, 2018
Messages
43
Hi,
How to save an excel file with password to open using vba. I am able to do it with SaveAs but it is prompting to replace the existing file. Is there a way to apply password to open workbook with save function?
 
Did you try this:

Code:
<code class="lang-vb x-hidden-focus">Application.DisplayAlerts = False 
your saveas code here
Application.DisplayAlerts = True</code>
 
Upvote 0
Hi, try this:
Rich (BB code):
Sub SaveWpWithPassword()
  ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Name, Password:="123"
End Sub
To suppress a warnimg message use Application.DisplayAlerts as it's suggested in post #2
 
Last edited:
Upvote 0
This is again saveas command that i am already using but saveas is actually replacing the existing file, that i don't want.
 
Upvote 0
Any saving replaces an existing file.
This saves file with a password without warning message appearing.
Rich (BB code):
Sub SaveWbWithPassword()
  Application.DisplayAlerts = False
  ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Name, Password:="123"
  Application.DisplayAlerts = True
End Sub
You may replace ActiveWorkbook.Name by something like "C:\Temp\Test.xlsx" if required.

Code to open workbook with a (file) password protection:
Rich (BB code):
Sub OpenWbWithPassword()
  Workbooks.Open Filename:="C:\Temp\Test.xlsx", Password:="123"
End Sub

Please describe the exact problem with those suggested code.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,771
Messages
6,192,926
Members
453,767
Latest member
922aloose

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