简体   繁体   English

Word VBA 强制另存为 .docm

[英]Word VBA force save as .docm

I've ended up cleaning up someone's mess on a Word document that's used throughout my company.我最终清理了整个公司使用的 Word 文档上的某人的烂摊子。 It is a macro-heavy document that needs to be saved as a .docm exclusively.它是一个需要单独保存为.docm的宏文件。 I'm worried about it getting "save as"-d as something other than a .docm , but I can't seem to find a way to limit the save as file picker or swap out the extension on save as while still using VBA.我担心它会“另存为”-d 作为.docm以外的其他.docm ,但我似乎无法找到一种方法来限制另存为文件选择器或在仍使用 VBA 的同时在保存时换出扩展名.

How can I achieve this?我怎样才能做到这一点?

Edit: Some of the things I've tried, to no avail: This has the right idea, but doesn't actually limit the filetypes down on save https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_other/how-to-set-path-for-wddialogfilesaveas-dialog/535b7f9c-9972-425c-8483-35387a97d61d编辑:我尝试过的一些事情,但无济于事:这是正确的想法,但实际上并没有限制保存https://answers.microsoft.com/en-us/msoffice/forum/ msoffice_word-mso_other/how-to-set-path-for-wddialogfilesaveas-dialog/535b7f9c-9972-425c-8483-35387a97d61d

Towards the bottom, Microsoft says that SaveAs isn't compatible with filter.clear and filter.add https://msdn.microsoft.com/en-us/library/office/aa219834(v=office.11).aspx在底部,微软表示 SaveAs 与 filter.clear 和 filter.add 不兼容https://msdn.microsoft.com/en-us/library/office/aa219834(v=office.11​​).aspx

In order to hijack the native "SaveAs" dialog in Word, you need to lever the Application-level event for DocumentBeforeSave , and then call the FileDialog manually, in order to validate the extension.为了劫持 Word 中的本机“另存为”对话框,您需要利用DocumentBeforeSave的应用程序级事件,然后手动调用 FileDialog,以验证扩展名。

1. Create a standard code module and name it modEventHandler . 1. 创建一个标准代码模块并将其命名为modEventHandler Put the following code in it.将以下代码放入其中。

Option Explicit
Public TrapFlag As Boolean
Public cWordObject As New cEventClass

'You may not need these in a DOCM, but I needed to implement this in an ADD-IN

Sub TrapEvents()
If TrapFlag Then
    Exit Sub
End If
Set cWordObject.DOCEvent = Application
TrapFlag = True

End Sub
Sub ReleaseTrap()
If TrapFlag Then
    Set cWordObject.DOCEvent = Nothing
    Set cWordObject = Nothing
    TrapFlag = False
End If
End Sub

2. Create a Class Module called cEventClass and put this code in the module: 2. 创建一个名为cEventClass的类模块并将此代码放入模块中:

Option Explicit
Public WithEvents DOCEvent As Application

Private Sub DOCEvent_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
' Do not prevent SAVEAS for *other* documents
If ObjPtr(Doc) <> ObjPtr(ThisDocument) Then
    Exit Sub
End If

If SaveAsUI Then
    ' The user has invoked SAVE AS command , so we will hijack this and use our own FileDialog
    Call CustomSaveAs(Doc)
    ' Prevent duplicate appearance of the SAVEAS FileDialog
    Cancel = True
End If
End Sub

Private Sub CustomSaveAs(ByRef Doc As Document)
Dim fd As FileDialog
Dim filename$

Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.Show
If fd.SelectedItems.Count = 0 Then Exit Sub
filename = fd.SelectedItems(1)
If Not Right(filename, 4) = "docm" Then
    ' ### DO NOT EXECUTE this dialog unless it matches our .DOCM file extension
    MsgBox "This document should only be saved as a DOCM / Macro-Enabled Document", vbCritical, "NOT SAVED!"
Else
    fd.Execute
End If
End Sub

3. In ThisDocument module, do the following code: 3. 在ThisDocument模块中,执行以下代码:

Private Sub Document_Close()
Call modEventHandler.ReleaseTrap
End Sub

Private Sub Document_Open()
Call modEventHandler.TrapEvents
End Sub

How Does This Work?这是如何运作的?

ThisDocument raises the Document_Open event which calls on the TrapEvents procedure. ThisDocument引发Document_Open事件,该事件调用TrapEvents过程。

TrapEvents procedure creates a WithEvents instance of Word.Application class, exposing additional events to automation. TrapEvents过程创建Word.Application类的WithEvents实例,向自动化公开其他事件。 One of these is DocumentBeforeSave .其中之一是DocumentBeforeSave

We use the DocumentBeforeSave event to trap the SaveAs operation.我们使用DocumentBeforeSave事件来捕获SaveAs操作。 If the User has requested a SaveAs , then we force the dialog with logic as created in CustomSaveAs procedure.如果用户请求了SaveAs ,那么我们将使用在CustomSaveAs过程中创建的逻辑强制对话框。 This uses simple logic to test the file extension provided, and prevents the document from being saved if it is not a DOCM extension.这使用简单的逻辑来测试提供的文件扩展名,并防止文档不是 DOCM 扩展名时被保存。 If the FileDialog does receive a valid DOCM extension, then we Execute the dialog which saves the file as the new name.如果 FileDialog 确实收到了有效的 DOCM 扩展名,那么我们Execute将文件保存为新名称的对话框。

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

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