Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save the document as .docx without the VBA code

Tags:

ms-word

vba

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
like image 427
user1966363 Avatar asked Nov 03 '22 06:11

user1966363


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

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
like image 78
html_programmer Avatar answered Nov 15 '22 05:11

html_programmer