5
votes

I created a template with VBA code and userform. I saved it as .dotm (macro-enabled template).

I want to open the template, use the interface to make changes in the document and after that save the document as .docx without references to the template/code. When I open the docx and open the visual basic editor I find there the code.

This is my code to exit from the interface

Private Sub Sair_Click()
ActiveDocument.Bookmarks("NomeProj").Range.text = Nomeproj.Value
ActiveDocument.TablesOfContents(1).Update
Application.Quit

End Sub
2

2 Answers

2
votes
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:

The VBA code is stored too. If you want to prevent this, the best you could do is move your text into a new document and save this document.
Simple example, based on the storage of a bookmark:

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
0
votes

A docx file cannot contain any VBA code. Period. However, every docx file has access to any code in whatever template it's attached to. By default, when you create a new document from a template, the document is attached to that template. If you don't want the resulting docx file to have any further access to the VBA in its attached template, attach it to a suitable dotx template. Assuming you have such a dotx template, you can do that via Developer|Document Template>Templates>Attach, or via the AttachedTemplate method.