将文档另存为不带 VBA 代码的 .docx

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14478865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:48:02  来源:igfitidea点击:

Save the document as .docx without the VBA code

vbaword-vba

提问by user1966363

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

我用 VBA 代码和用户表单创建了一个模板。我将它保存为 .dotm(启用宏的模板)。

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.

我想打开模板,使用界面在文档中进行更改,然后将文档另存为 .docx 而不引用模板/代码。当我打开 docx 并打开 Visual Basic 编辑器时,我在那里找到了代码。

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

回答by Trace

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:

VBA 代码也被存储。如果您想防止这种情况发生,最好的办法就是将您的文本移动到一个新文档中并保存该文档。
简单的例子,基于书签的存储:

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

回答by macropod

A docx file cannotcontain 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.

docx 文件不能包含任何 VBA 代码。时期。但是,每个 docx 文件都可以访问它附加到的任何模板中的任何代码。默认情况下,当您从模板创建新文档时,该文档将附加到该模板。如果您不希望生成的 docx 文件在其附加模板中进一步访问 VBA,请将其附加到合适的 dotx 模板。假设您有这样一个 dotx 模板,您可以通过 Developer|Document Template>Templates>Attach 或通过 AttachedTemplate 方法来实现。