[英]Error Message “Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed” when saving with VBA password protection on
I am trying to save a single sheet from a workbook as a.txt file- the code I have to do this is:我正在尝试将工作簿中的单个工作表保存为 a.txt 文件-我必须这样做的代码是:
ActiveWorkbook.SaveAs Filename:= _
directory & Application.UserName & "_" & file_name _
, FileFormat:=xlUnicodeText, CreateBackup:=False
(directory and file-name are defined earlier in the macro) (目录和文件名在宏前面定义)
The macro that contains this code is password protected.包含此代码的宏受密码保护。 When the macro is run without entering the password, I receive the error message "Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed".
在未输入密码的情况下运行宏时,我收到错误消息“运行时错误'1004':对象'_Workbook'的方法'SaveAs'失败”。 When I open up visual basic and enter the password, the macro runs fine with no error messages.
当我打开 Visual Basic 并输入密码时,宏运行良好,没有错误消息。 Is there a different syntax I can use to save the.txt file, or does anyone know what could be causing this error?
我可以使用不同的语法来保存 .txt 文件,或者有人知道可能导致此错误的原因吗?
I found an old help post on another site from 2006, with 0 answers:我在 2006 年的另一个网站上找到了一篇旧的帮助帖子,答案为 0:
https://www.excelforum.com/excel-general/567235-error-when-save-as-csv-with-vba-password-protection-on.html https://www.excelforum.com/excel-general/567235-error-when-save-as-csv-with-vba-password-protection-on.html
Any help/advice would be greatly appreciated!任何帮助/建议将不胜感激!
Not sure if answering my own question is allowed or is the correct thing to do - but I managed to get the macro working and hopefully this will help someone else in the future:不确定是否允许回答我自己的问题或者是正确的做法 - 但我设法让宏工作,希望这将在未来对其他人有所帮助:
Instead of:代替:
ActiveWorkbook.SaveAs Filename:= _ directory & Application.UserName & "_" & file_name _, FileFormat:=xlUnicodeText, CreateBackup:=False
Use:利用:
ThisWorkbook.SaveAs Filename:=direct & file_name, FileFormat:=xlText
Not sure why this works and the previous code doesn't but it will save a.txt file even when the vba is password protected.不知道为什么这会起作用并且之前的代码不起作用,但即使 vba 受密码保护,它也会保存 a.txt 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.