“SaveAS” macro in excel with password protection

被刻印的时光 ゝ 提交于 2019-12-16 18:02:59

问题


Hi I have this macro in excel file:

ActiveWorkbook.SaveAs filename:="C:\SAP Imports\Sales Orders\" & Range("A1") & ".xlsm", FileFormat:= _
        xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
        , CreateBackup:=False

This macro is password protected. Whenevr I run this I get "run time" error and I am not able to figure out whats wrong with my code.

Should I write my password instead of writing "". Please advise.


回答1:


If the directory does not exist, the macro will fail.

Ensure "C:\SAP Imports\Sales Orders\" exists before running the macro.




回答2:


I set up a workbook with the same thing and when I protected the macro and placed the password in the lines you showed then it saved just fine.

This led to a second problem, however, when I tried to open up the other workbook. I then got an error saying that the file was an incorrect format. I changed your line by deleting the ".xlsm" and changing the fileformat to fileformat:=52. This ended up making it

ActiveWorkbook.SaveAs filename:="C:\SAP Imports\Sales Orders\" & Range("A1"), FileFormat:=52, Password:="password", WriteResPassword:="password", ReadOnlyRecommended:=False, CreateBackup:=False 

When I changed it to this then the workbook saved just fine. When I opened up the saved book it prompted me for the passwords that were entered above.



来源:https://stackoverflow.com/questions/8247735/saveas-macro-in-excel-with-password-protection

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