如何禁用' save' Excel中的选项,但'另存为'应该仍在工作

时间:2016-01-29 23:39:11

标签: excel vba excel-vba

我需要在Excel中禁用保存选项,但我仍然需要另存为选项才能正常工作...所以我知道如何通过此VBA禁用这两个选项:

Private Sub Workbook_BeforeSave(ByVal SaveUI As Boolean, Cancel As Boolean)
    MsgBox "You can't save this workbook!"
    Cancel = True    
End Sub

但是如何禁用保存却可以让我们保持工作状态。 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

尝试:

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

修改

要从ThisWorkbook类模块删除代码(删除所有内容),我们可以使用此代码:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

 If SaveAsUI = False Then
  MsgBox "You can't save this workbook!"
  Cancel = True

 Else
  With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
   .DeleteLines 1, .CountOfLines
  End With

 End If
End Sub

注:

  • 这将删除ThisWorkbook课程模块中的所有内容,而不仅仅是Workbook_BeforeSave
  • 我们必须允许信任访问VBA项目对象模型(文件>选项>单击信任中心,单击信任中心设置,然后单击宏设置)。