Share via

Using VBA: remove code from .docm, save it in .docx

Anonymous
2016-08-18T17:45:30+00:00

We have a form that users generate from a template (.dotm template). After filling in the form, the user click a submit button on the form self to send it as attachment via email. It works, but it sends the document with the VBA code behind the submit button. When the attachment is opened at the other end, Word warn about security, Read only, etc.

The VBA code behind the submit button:

  • Save the document
  • Create the email (outlook 2013 application)
  • Send it automatically

How can we remove all codes and save it as a valid .docx document using VBA? If we use “document.SaveAs2” method and change the extension to .docx, the code remain behind document.

Is there other solutions?

Thanks

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Stefan Blom 342.4K Reputation points MVP Volunteer Moderator
2016-08-18T19:20:57+00:00

Indeed, changing the filename extension is not enough; you must also choose the correct file format.

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-08-18T19:06:02+00:00

    SOLVED

    It was code error. I changed the following code and it works.

    • dDoc.SaveAs2 FileName:=sPath, fileformat:=wdFormatDocumentDefault
    • .Attachments.Add sPath

    Thanks

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-08-18T18:29:22+00:00

    The code is not removed by saving it as .docx. My code is:

    Public Sub btnSubmit_Click()

    Dim OL As Object

    Dim EmailItem As Object

    Dim dDoc As Document

    Dim sPath As String

    Application.ScreenUpdating = False

    Set OL = CreateObject("Outlook.Application")

    Set EmailItem = OL.CreateItem(0)

    Set dDoc = ActiveDocument

    sPath = "c:\_dump\TESTsave3.docx"

    dDoc.SaveAs2 sPath

    With EmailItem

        .Subject = "Insert Subject Here"

        .Body = "TES TEST"

        .To = "******@outlook.com"

        .Attachments.Add dDoc.FullName

    '.Send

        .Display

    End With

    Application.ScreenUpdating = True

    Set dDoc = Nothing

    Set OL = Nothing

    Set EmailItem = Nothing

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Stefan Blom 342.4K Reputation points MVP Volunteer Moderator
    2016-08-18T18:16:49+00:00

    Saving the file as a macro-free document (*.docx) will delete any code that is present. However, it is better to keep all the code in the template instead. When the macro-free document is created, it can still access the code in the attached template.

    Was this answer helpful?

    0 comments No comments
  4. Charles Kenyon 167.7K Reputation points Volunteer Moderator
    2016-08-18T18:16:22+00:00

    Save the document as a .docx document in your code, or...

    Consider, instead of a submit button, either a QAT icon linked to a macro in the template or a MacroButton Field that you can have look like a button.

    Was this answer helpful?

    0 comments No comments