Word VBA调用SaveAs出现运行时错误6294:文件类型与扩展名不兼容
报错根因
运行时错误6294是SaveAs方法参数使用错误触发的,你的代码存在两个核心问题:
- VBA中命名参数的赋值符号必须使用
:=而不是= - Word VBA中对应文档打开密码的参数名为
PasswordDocument,高版本Word不识别简写的Password参数
修正后代码
适配2021版Word VBA(V7.1)的完整代码如下,优先使用高版本Office推荐的SaveAs2方法提升兼容性:
Dim myFile As String Dim PathToUse As String Dim SavePath As String Dim myDoc As Document Dim strPassword As String strPassword = "current_password" ' 统一路径末尾的反斜杠,避免拼接文件名时出错 PathToUse = "C:\source\" SavePath = "C:\destination\" myFile = Dir$(PathToUse & "*.docx") While myFile <> "" ' 打开加密文档 Set myDoc = Documents.Open(FileName:=PathToUse & myFile, PasswordDocument:=strPassword) ' 修正参数写法,明确指定清空密码 myDoc.SaveAs2 FileName:=SavePath & myFile, FileFormat:=wdFormatXMLDocument, PasswordDocument:="" myDoc.Close SaveChanges:=wdDoNotSaveChanges ' 读取下一个文件 myFile = Dir$() Wend
额外注意事项
- 运行前请确认目标文件夹
C:\destination\已提前创建,不存在会触发路径不存在错误 - 如果需要兼容更低版本Word,可以把
SaveAs2替换回SaveAs,保持参数写法为PasswordDocument:=""即可 - 可自行添加错误捕获逻辑,遇到密码不匹配、文件被占用的情况可以跳过当前文件继续执行,不会直接中断整个批量任务
内容的提问来源于stack exchange,提问作者AdamJones
相关产品推荐
相关产品推荐

