简体   繁体   English

使用 VBA 密码保护进行保存时出现错误消息“运行时错误'1004':对象'_Workbook'的方法'SaveAs'失败”

[英]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.

相关问题 运行时错误“1004”:对象“_Workbook”的方法“SaveAs”失败 - Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed 运行时错误'1004':对象'_Workbook的SaveAs方法失败 - Run-time error '1004': SaveAs Method of object '_Workbook failed 运行时错误'1004'对象'_Workbook'的方法'保存'失败 - Run-time error '1004' Method 'Save' of object '_Workbook' failed Excel 2007 VBA ActiveWorkbook另存为不保存…运行时错误1004 - Excel 2007 VBA ActiveWorkbook SaveAs not saving … Run-time error 1004 运行时错误1004-对象范围失败-VBA 2013 - Run-time error 1004 - Range of object failed - VBA 2013 将工作表复制到 csv - VBA 时出错:object“范围”的方法“值”失败(运行时错误“1004”) - Error while copying sheet to csv - VBA: Method 'Value' of object 'Range' failed (Run-time error '1004') 错误消息 = 运行时错误“1004”:方法“object '_Global' 的范围”失败 - Error message = Run-time error '1004': Method 'Range of object '_Global' failed 运行时错误'1004':保存文件后,对象'_Worksheet'的方法'Range'失败 - Run-time error '1004': Method 'Range' of object '_Worksheet' failed after saving the file VBA 运行时错误“1004”:object“_Worksheet”的方法“范围” - VBA Run-time error '1004': Method 'Range' of object '_Worksheet' Excel - “对象的运行时错误‘1004’方法‘SaveAs’_‘workbook’失败”Macbook - Excel - "Runtime error '1004' Method 'SaveAs' of Object' _'workbook' Failed" Macbook
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM