Save the document as .docx without VBA code

I created a template with VBA code and a custom form. I saved it as .dotm (a template with macro support).

I want to open a template, use the interface to make changes to the document, and after that save the document in .docx format without links to the template / code. When I open docx and open the Visual Basic Editor, I find the code there.

This is my code to exit the interface

Private Sub Sair_Click() ActiveDocument.Bookmarks("NomeProj").Range.text = Nomeproj.Value ActiveDocument.TablesOfContents(1).Update Application.Quit End Sub 
+4
source share
2 answers
 ActiveDocument.SaveAs FileName:="Test.docx", FileFormat:= _ wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _ :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _ :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _ SaveAsAOCELetter:=False ActiveDocument.Convert 'Required if not Word 2007 format 

Edit:

VBA code is also saved. If you want to prevent this, it is best to move the text to a new document and save this document.
A simple example based on bookmark storage:

 Option Explicit Sub Save_Doc_NoMacros() Dim ThisDoc As Word.Document Dim oDoc As Word.Document Set ThisDoc = ActiveDocument ThisDoc.Bookmarks("Bookmark1").Select Selection.Copy Set oDoc = Documents.Add Selection.Paste oDoc.SaveAs FileName:="U:/Text.docx", FileFormat:=wdFormatDocument oDoc.Close End Sub 
+1
source

The docx file cannot contain VBA code. Period. However, each docx file has access to any code in any template to which it is attached. By default, when creating a new document from a template, the document is attached to this template. If you do not want the resulting docx file to have further access to VBA in its attached template, attach it to a suitable dotx template. Assuming you have such a dotx pattern, you can do it through Developer | Document Template> Templates> Attach or through the AttachedTemplate method.

0
source

All Articles