Disable Excel save option but allow macro save

邮差的信 提交于 2019-12-18 09:54:08

问题


I'm creating an excel file and I want to disable the 'save' and 'save as...' option.

I found a lot of solutions on the internet, like this one in VBA:

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

It prevents user from saving changes, but I can't save my changes and that's the problem because I need to do more changes in the VBA code.

Is there a way to save my macro changes ? Like an administrator mode etc... ?

Thank you for your future answers.


回答1:


Use a global variable to override the Save disable:

    Dim override as Boolean

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
         if Not(override) then
           MsgBox "You can't save this workbook!"
           Cancel = True
         end if
    End Sub
    Sub SaveMyChanges()
       override = true
       ActiveWorkbook.Save
       override = false
    End Sub



回答2:


You can also save while in Design Mode in VBA.

Sorry for the necro, but this works also and is what I do.



来源:https://stackoverflow.com/questions/30806618/disable-excel-save-option-but-allow-macro-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!