Piston
Piston

Reputation: 95

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. 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:="[email protected]"
        .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.

Upvotes: 1

Views: 2078

Answers (2)

macropod
macropod

Reputation: 13515

Since a document created from a template won't contain any macros, there's no point in saving it in the docm format. 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:

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

and reference that for your email attachment.

Upvotes: 0

freeflow
freeflow

Reputation: 4355

You should start by checking the SaveAs method.

You should have noticed that when you typed the '.' after ActiveDocument that SaveAs was not present in the list of intellisense options. Instead you get offered SaveAs2.

The reason for this is that SaveAs is deprecated and has been superseded by SaveAs2. The old method is still available but its continued presence in future releases of Office/VBA cannot be guaranteed.

SaveAs2 on its own just adds a new option (compatibility mode) to the SaveAs method. Neither SaveAs2 or the ability to choose compatibility will resolve your problem. Instead you have to use 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). 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.

https://learn.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

The format in which the document is saved. Can be any WdSaveFormat constant. To save a document in another format, specify the appropriate value for the SaveFormat property of the FileConverter object.

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).

For you to save your document as a macro enabled word template you would need to specify the enumeration constant , wdFormatXMLTemplateMacroEnabled.

e.g.

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).

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.

Upvotes: 1

Related Questions