VBA 将文档从 .docm 保存(并转换)为 .docx

标签 vba ms-word save save-as

我在项目结束时有以下代码:

'Save the Document
Dim Directory As String, FileName As String
Directory = "C:\Users\" & (Environ$("Username")) & _
    "\Desktop\STL\"
If Len(Dir(Directory, vbDirectory)) = 0 Then
    MkDir Directory
End If
FileName = sDNUM & " " & Format(Date, "YYYY-MM-DD") & ".docx"
SaveAs Directory & FileName


MsgBox "File saved to:" & vbNL & Directory & FileName

并且我试图在运行启用宏的工作簿后将文件保存为 docx(未启用宏)。

问题是通过使用上述方法保存我的文件,在尝试打开新保存的文件时,我收到以下错误消息:

The file < filename > cannot be opened because there are problems with the contents.

我应该用什么方法来正确保存这些文档?

好奇的杂项注释:

vbNL 只是 vbNewLine 的一个函数。我经常使用它,我想我只是懒得一直输入它,所以我做了一个函数来缩短文本。

sDNUM 也只是另一个与问题无关的函数。

最佳答案

使用 docx 扩展名保存文件不会自动将其转换为非启用宏。

改变

SaveAs Directory & FileName

SaveAs2 Directory & FileName, wdFormatXMLDocument

有关 SaveAs2 方法的详细信息,请参阅 here .

关于VBA 将文档从 .docm 保存(并转换)为 .docx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44006947/

相关文章:

vba - 运行时错误 424 - 需要对象错误

java - 如何在 Apache POI Word 中添加图片交叉引用?

r - 使用 ggsave/sprintf 出现奇怪的错误消息

c# - File.WriteAllText 问题

vba - 以当前日期关闭宏

python - 'List' 对象没有属性 'Values' 错误

c# - 我是否必须关闭 Word Interop 应用程序?

node.js - npm 命令行工具的状态保存在哪里?

Excel VBA : how to check for calculated column?

c# - 使用 OpenXML 更改段落中的颜色文本