简体   繁体   中英

Save the document as .docx without the VBA code

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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