VBA to create password protected file

[亡魂溺海] 提交于 2020-01-26 01:28:09

问题


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")

回答1:


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.



来源:https://stackoverflow.com/questions/43752758/vba-to-create-password-protected-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!