簡體   English   中英

如何防止用戶將Word文檔另存為另一個文件

[英]How do I prevent users from Saving word document as another file

在保存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

您只需要測試If SaveAsUI Then並且僅在SaveAsUITrue時才取消:

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

但是請注意,這根本不安全,很容易被欺騙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM