简体   繁体   English

使用 SaveAs2 保存时如何从 word 文件中删除宏?

[英]How can I delete macros from word-file when saving with SaveAs2?

So, I have a Word-Template with Macros inside.所以,我有一个带有宏的 Word 模板。 What do I want to achieve?我想要达到什么目标?

When the File is saved I want to save the Word-File as a new docx-File with new name and new destination.保存文件后,我想将 Word 文件另存为具有新名称和新目标的新 docx 文件。

When I click save now the file is already saved correctly with the new name and new destination (also as a *docx file) but the macro is still inside so when I press save now the macro is still executed (this is what I want to remove).当我单击“立即保存”时,文件已使用新名称和新目标(也作为 *docx 文件)正确保存,但宏仍在其中,因此当我现在按“保存”时,宏仍会执行(这就是我想要的)消除)。

So how can I remove when saving the File all the macros in the new file?那么如何在保存文件时删除新文件中的所有宏?

My current code is:我目前的代码是:

Sub FileSave()
'
' saveFile Macro
'
'
    DocDate = ActiveDocument.Tables(1).Cell(4, 2)
    GCCName = ActiveDocument.Tables(1).Cell(8, 1)
    FileExtension = ".docx"
    matchedStr = GCCName & " - " & DocDate & FileExtension

    matchedStr = Replace(matchedStr, "", "")
    matchedStr = Replace(matchedStr, Chr(13), "")

    ActiveDocument.Save
    ChangeFileOpenDirectory "########\"
    ActiveDocument.SaveAs2 FileName:= _
        matchedStr, FileFormat:= _
        wdFormatXMLDocument, LockComments:=False, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
End Sub

If you have a Word template (dotm) you should not open the template to create a document: you should just create a new document right from the start.如果您有 Word 模板 (dotm),则不应打开该模板来创建文档:您应该从一开始就创建一个新文档。 If the new document is being generated by code then use Documents.Add rather than Documents.New .如果新文档是由代码生成的,则使用Documents.Add而不是Documents.New The new document will contain no macros and can be saved as docx with no problem.新的文件将包含宏,并可以保存为没有问题的docx。

These days, it is possible to save a template file to a file with the docx extension and to a "document without macros" file format - that will work.现在,可以将模板文件保存为具有 docx 扩展名的文件和“没有宏的文档”文件格式 - 这将起作用。 (In earlier versions of Word, with the *.dot file format, it did not.) (在早期版本的 Word 中,使用 *.dot 文件格式,它没有。)

HOWEVER, the document will still have a link to the template and be able to "see" the macros via that link.但是,该文档仍将具有指向模板的链接,并且能够通过该链接“查看”宏。 This may be why you think the macros are being saved in the docx document.这可能就是您认为宏被保存在 docx 文档中的原因。 If such a file is sent "out of house" (to a location with no way to access the template in the folder where it's stored) then the macros will not show up or be available.如果这样的文件被发送到“外部”(发送到无法访问其存储文件夹中的模板的位置),则宏将不会显示或可用。

If you want to completely dissociate the document from its template, attach it to a different template (usually Normal.dotm because that's available on all Word installations).如果您想将文档与其模板完全分离,请将其附加到不同的模板(通常是 Normal.dotm,因为它可用于所有 Word 安装)。 Example based on the code in the question:基于问题中的代码的示例:

ActiveDocument.AttachedTemplate = NormalTemplate

This needs to be at the end of the code since it will dissociate it from the code that's performing the action.这需要在代码的末尾,因为它将与正在执行操作的代码分离。

A tip for the code in the question: working with ActiveDocument can be unreliable - it's not certain that it will always be the document intended (the user could activate a different one, for example).问题中的代码提示:使用ActiveDocument可能不可靠 - 不确定它是否始终是预期的文档(例如,用户可以激活不同的文档)。 A more reliable approach is to work with a Document object.更可靠的方法是使用Document对象。 The same applies to other things, such as tables.这同样适用于其他事物,例如表格。 For example:例如:

Dim doc as Word.Document
Dim tbl as Word.Table
Set doc = ActiveDocument
' do things with the document...
Set tbl = doc.Tables(1)
' do things with the table
DocDate = tbl.Cells(4,2).Range.Text
GCCName = tbl.Cell(8, 1).Range.Text
doc.Save

Alternative for docm files, taken from my project从我的项目中获取的 docm 文件的替代方案

NameNoExtension is the name of the new file. NameNoExtension 是新文件的名称。 Put your path where SelectSaveDir() is in my code.将您的路径放在我的代码中 SelectSaveDir() 的位置。 The code opens a copy of the file where the the code is, deletes all the vba Modules and saves the copied file as docx.代码会打开代码所在文件的副本,删除所有 vba 模块并将复制的文件保存为 docx。

Sub SaveCopyAsDocx(ByVal NameNoExtension As String)
    Dim NewDocument As Object
    Set NewDocument = Documents.Add(ThisDocument.FullName)

    Dim Modul As Object

    For Each Modul In NewDocument.VBProject.VBComponents
        If Modul.Name <> "ThisDocument" Then
            NewDocument.VBProject.VBComponents.Remove Modul
        End If
    Next

    NewDocument.SaveAs SelectSaveDir() & "\" & NameNoExtension & ".docx"

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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