vba 带有密码保护的 Excel 中的“SaveAS”宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8247735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"SaveAS" macro in excel with password protection
提问by Nupur
Hi I have this macro in excel file:
嗨,我在excel文件中有这个宏:
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.
我应该写我的密码而不是写“”。请指教。
回答by Justin Self
If the directory does not exist, the macro will fail.
如果目录不存在,宏将失败。
Ensure "C:\SAP Imports\Sales Orders\" exists before running the macro.
在运行宏之前确保“C:\SAP Imports\Sales Orders\”存在。
回答by Jon
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
然而,当我试图打开另一个工作簿时,这导致了第二个问题。然后我收到一条错误消息,说文件格式不正确。我通过删除".xlsm"
并将文件格式更改为fileformat:=52
. 这最终做到了
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.
当我将其更改为这个时,工作簿保存得很好。当我打开保存的书时,它提示我输入上面输入的密码。