在保存Microsoft Word文档之前,必须设置一个必填字段。
Public Sub FileSave()
Dim orng As Word.Range
Dim ofld As FormFields
Set orng = ActiveDocument.Range
Set ofld = orng.FormFields
For i = 1 To ofld.Count
ofld(i).Select
If ofld(i).Result = "" Then
MsgBox "Please ensure that all are filled."
Exit Sub
End If
Next i
ActiveDocument.Save
End Sub
此代码仅防止保存文档而不填写表格。它不会阻止用户以其他格式保存文档。如何修改它,使用户也无法另存为另一个文档?
为了提供一些上下文,以前我在模块级别有另一个代码,阻止用户将文档另存为另一种格式。问题在于它也完全阻止用户保存文档。我正在尝试找到一种解决方案,以防止用户另存为其他名称和/或格式,并且仅在填写所有必填字段后才允许他们保存文档。如果用户停用宏,则文档对他们实际上是无用的,因此不必担心。我的查询与用户启用宏并访问文档后有关
Private WithEvents App As Word.Application
Private Sub Document_Open()
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveasUI As Boolean, Cancel As Boolean)
Cancel = True
MsgBox ("You are not allowed to save this file as another document")
End Sub
答案 0 :(得分:1)
您只需要测试If SaveAsUI Then
并仅在SaveAsUI
为True
时取消:
Option Explicit
Private WithEvents App As Word.Application
Private Sub Document_Open()
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
Cancel = True
MsgBox ("You are not allowed to save this file as another document")
End If
End Sub
但是请注意,这根本不安全,很容易被欺骗。