简体   繁体   中英

VBA to create password protected file

With the below coding, I am able to create a copy of password protected file. But I also want to put the password in newly created file. How can I add password while creating new excel workbook.

Workbooks.Open Filename:=myFileNameDir, Password:="yourpassword", UpdateLinks:=0
Set ws1 = Worksheets("OJT Plan")
ws1.Activate
filen = TextBox3.Text & "_" & TextBox59 & "_" & VBA.Format(Now, "MMddyyyyhmmss AM/PM ")
ws1.SaveAs Sheet1.Range("V3").Value & filen & ".xlsx"
Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = "Details"
Set ws13 = Worksheets("Details")

If you look at the documentation you will see there are two password arguments for the SaveAs command

ws1.SaveAs FileName:=Sheet1.Range("V3").Value & filen & ".xlsx", _
           Password:="yourpassword", _                                
           WriteResPassword:="yourreadonlypassword"

The difference in these two password arguments is explained in the docs:

Password : A case-sensitive string (no more than 15 characters) that indicates the protection password to be given to the file.

WriteResPassword : A string that indicates the write-reservation password for this file. If a file is saved with the password and the password isn't supplied when the file is opened, the file is opened as read-only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM