简体   繁体   English

无法打开用 VBA 保存的 Word .docm 文档

[英]Cannot open Word .docm document saved with VBA

I am trying to save a Word Macro-enabled template and then send the resulting file via email, but the file saved (and sent) as .docm cannot be opened.我正在尝试保存启用 Word 宏的模板,然后通过电子邮件发送生成的文件,但无法打开保存(和发送)为 .docm 的文件。 It throws this error:它抛出这个错误:

"We're sorry. We can't open B.docm because we found a problem with its contents"

Do I need to do something to "convert" the document?我需要做些什么来“转换”文档吗? Couldn't find anything about it and everyone seems to agree that the way I save the document is fine.找不到任何关于它的信息,每个人似乎都同意我保存文档的方式很好。

This is the code I am using:这是我正在使用的代码:

Private Sub CommandButton1_Click()

'Save Document
    Dim wdApp As Word.Application
    Set wdApp = GetObject(, "Word.Application")

    wdApp.ActiveDocument.SaveAs "H:\Word\B.docm"


'Send Email
    Dim outlook         As outlook.Application
    Dim maiMessage      As outlook.MailItem        

    Set outlook = New outlook.Application
    Set maiMessage = outlook.CreateItem(olMailItem)
    With maiMessage
        .Subject = "Sent"
        .Recipients.Add Name:="name@company.com"
        .Attachments.Add Source:="H:\Word\B.docm"
        .Send
    End With

End Sub

More info:更多信息:

- If I save the document by normal means (File > Save as > .docm), it works.
- If I save the document using the macro but with .docx extension, it also works.

You should start by checking the SaveAs method.您应该首先检查 SaveAs 方法。

You should have noticed that when you typed the '.'当您输入“.”时,您应该已经注意到了。 after ActiveDocument that SaveAs was not present in the list of intellisense options.在 ActiveDocument 之后,智能感知选项列表中不存在 SaveAs。 Instead you get offered SaveAs2.相反,您会得到 SaveAs2。

The reason for this is that SaveAs is deprecated and has been superseded by SaveAs2.这样做的原因是 SaveAs 已被弃用并已被 SaveAs2 取代。 The old method is still available but its continued presence in future releases of Office/VBA cannot be guaranteed.旧方法仍然可用,但不能保证它在未来版本的 Office/VBA 中继续存在。

SaveAs2 on its own just adds a new option (compatibility mode) to the SaveAs method. SaveAs2 本身只是向 SaveAs 方法添加了一个新选项(兼容模式)。 Neither SaveAs2 or the ability to choose compatibility will resolve your problem. SaveAs2 或选择兼容性的能力都不能解决您的问题。 Instead you have to use F1.相反,您必须使用 F1。

In the VBA IDE placing the cursor on any keyword and pressing F1 will bring up the MS help page for that keyword (if the keyword exists).在 VBA IDE 中,将光标放在任何关键字上并按 F1 将打开该关键字的 MS 帮助页面(如果该关键字存在)。 If you press F1 on SaveAs you won't get any help, jest a generic page, but if you press it whilst on SaveAs2 you will get this help page.如果你在 SaveAs 上按 F1 你不会得到任何帮助,开玩笑一般页面,但是如果你在 SaveAs2 上按它你会得到这个帮助页面。

https://docs.microsoft.com/en-us/office/vba/api/word.saveas2?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev11.query%3FappId%3DDev11IDEF1%26l%3Den-US%26k%3Dk(vbawd10.chm158007864)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv16)%26rd%3Dtrue https://docs.microsoft.com/en-us/office/vba/api/word.saveas2?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev11.query%3FappId%3DDev11IDEF1%26l% 3Den-US%26k%3Dk(vbawd10.chm158007864)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv16)%26rd%3Dtrue

When you look at the help page you will see that the second parameter for the SaveAs2 method is 'FileFormat' which is当您查看帮助页面时,您将看到 SaveAs2 方法的第二个参数是“FileFormat”,它是

The format in which the document is saved.文档的保存格式。 Can be any WdSaveFormat constant.可以是任何 WdSaveFormat 常量。 To save a document in another format, specify the appropriate value for the SaveFormat property of the FileConverter object.要以另一种格式保存文档,请为 FileConverter 对象的 SaveFormat 属性指定适当的值。

The fact that SaveAs (Now SaveAs2) didn't complain when you only provided a filename means that a default option has been supplied by the method, which is most likely to be the ' save this document as a word format document' option (wdFormatDocumentDefault). SaveAs (Now SaveAs2) 在您只提供文件名时没有抱怨这一事实意味着该方法提供了一个默认选项,这很可能是“将此文档另存为 word 格式文档”选项 (wdFormatDocumentDefault )。

For you to save your document as a macro enabled word template you would need to specify the enumeration constant , wdFormatXMLTemplateMacroEnabled.要将文档另存为启用宏的 Word 模板,您需要指定枚举常量 wdFormatXMLTemplateMacroEnabled。

eg例如

wdApp.ActiveDocument.SaveAs "H:\Word\B.docm", wdFormatXMLTemplateMacroEnabled

You would also have got to this option if you had recorded a macro during which you saved a document as a template to see what Word would do in the same situation (admittedly not always helpful).如果您录制了一个宏,在此期间您将文档另存为模板,以查看 Word 在相同情况下会做什么(不可否认,这并不总是有用),那么您也必须使用此选项。

Sub Macro1()
'
' Macro1 Macro
'
'
    ChangeFileOpenDirectory _
        "C:\Users\user1\Documents\Custom Office Templates\"
    ActiveDocument.SaveAs2 FileName:= _
        "C:\Users\user1\Documents\Custom Office Templates\test.dotm", FileFormat _
        :=wdFormatXMLTemplateMacroEnabled, LockComments:=False, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
End Sub

Good luck with your further endeavours.祝你的进一步努力好运。

Since a document created from a template won't contain any macros, there's no point in saving it in the docm format.由于从模板创建的文档不包含任何宏,因此将其保存为 docm 格式毫无意义。 Furthermore, unless you actually specify the file format in the SaveAs line, all you'll get is the native format (docx) in which case, all you need do is change the extension to suit:此外,除非您在 SaveAs 行中实际指定文件格式,否则您将获得的只是本机格式 (docx),在这种情况下,您需要做的就是更改扩展名以适应:

wdApp.ActiveDocument.SaveAs "H:\\Word\\B.docx" wdApp.ActiveDocument.SaveAs "H:\\Word\\B.docx"

and reference that for your email attachment.并将其作为您的电子邮件附件参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM