我在项目结束时有以下代码:
'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/