问题
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