预防工作簿保存但保存在macros中

我正在写一个代码,以防止用户保存工作簿,它只会保存在我想要的时间。 这是为了防止用户在不应该的时候进行更改和保存。 我已经创build了两个私人潜艇,但是当我自己保存工作簿时,我不知道该怎么办。 我希望能够将保存代码放在各种macros中,以便我可以随时控制保存。

以下是我的代码:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) MsgBox "You can't save this workbook!" Cancel = True End Sub Private Sub Workbook_Open() Dim myValue As String Dim Answer As String Dim MyNote As String MsgBox "Welcome to the Lot Input Program" If Range("A1").Value = "" Then Line: myValue = InputBox("Please input your email address:", "Input", "x@us.tel.com") 'Place your text here MyNote = "Is this correct?: " & myValue 'Display MessageBox Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Confirmation") If Answer = vbNo Then 'Code for No button Press GoTo Line Else Range("A1").Value = myValue End If ActiveWorkbook.Save End If End Sub 

你可以尝试这样的事情…

 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) If Environ("UserName") <> "YourUserNameHere" Then MsgBox "You can't save this workbook!" Cancel = True End If End Sub 

编辑:

 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim Ans As VbMsgBoxResult Ans = MsgBox("You can't save this workbook!" & vbNewLine & _ "Do you have password to save the file?", vbQuestion + vbYesNo) If Ans = vbYes Then frmPassword.Show 'UserForm to accept the password Else Cancel = True End If End Sub 

我添加了保存取消代码中引用的公共variablessaveLock。 这使我可以locking和解锁我的代码保存。 如果有人有更好的方法,请让我知道,但这确实解决了这个问题。

 Public saveLock As Integer Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) If saveLock = 0 Then Cancel = True End If End Sub Private Sub Workbook_Open() Dim myValue As String Dim Answer As String Dim MyNote As String saveLock = 0 MsgBox "Welcome to the Lot Input Program" If Range("A1").Value = "" Then Line: myValue = InputBox("Please input your email address:", "Input", "x@us.tel.com") 'Place your text here MyNote = "Is this correct?: " & myValue 'Display MessageBox Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Confirmation") If Answer = vbNo Then 'Code for No button Press GoTo Line Else Range("A1").Value = myValue End If saveLock = 1 ActiveWorkbook.Save saveLock = 0 End If End Sub