【问题标题】:Excel 2013 toggle read onlyExcel 2013 切换只读
【发布时间】:2015-04-10 17:04:13
【问题描述】:

我有一个使用 Excel 2003 编写的应用程序,最近升级到 Excel 2013。在 workbook_open 事件中,我将工作簿设置为只读 ActiveWorkbook.ChangeFileAccess xlReadOnly 并使用 Toggle Read Only 按钮在读\写和只读。在 Excel 2003 中,当切换文件模式时,工作簿会按预期切换。当我在 2013 年将它作为 .xlsm 运行时,在切换文件状态后会调用 Workbook_Open 事件并再次变为只读。

Private Sub Workbook_Open()
    If ActiveWorkbook.ReadOnly = False Then
        ActiveWorkbook.Saved = True
        ActiveWorkbook.ChangeFileAccess xlReadOnly
    End If
End Sub

【问题讨论】:

  • 切换时,您可以向单元格(或名称,即在名称管理器中)写入状态应该是什么,然后在Workbook_Open 事件中检查状态应该是什么,然后相应地打开。
  • @guitarthrower:工作簿在只读模式下无法保存更改。当模式从只读切换到读写时,由于工作簿的完全重新加载,我也会丢失所有变量。
  • [slaps forehead] 当然你不能以只读模式保存 :) 听起来当它切换时,它正在重新打开文件(我没有 2013,所以我可以' t 检验)。如果是这种情况,您可以尝试在代码中的某个位置实现Application.EnableEvents = False。另一种选择可能是在打开时检查当前状态。如果您可以发布您的 Workbook_Open 代码,将会有所帮助。
  • @guitarthrower:我创建了一个新工作簿,其中仅包含此代码(附加到问题中)来测试是否有其他原因导致问题。我能够复制错误。我遇到了使用窗口参数在工作簿之间传递变量,无论它们是否打开,这甚至都行不通。看来,当文件切换为只读时,其自身已终止。每次为工作簿分配一个新的 HWnd。

标签: vba excel excel-2003 excel-2013


【解决方案1】:

无需将 FileAccess 更改为只读即可获得您想要的结果。您可以使用 Workbook_BeforeSave 和 Workbook_Beforeclose 事件来控制保存工作簿的能力。我在下面提供了一个完整的代码示例,我相信它会满足您的需求。您可以使用切换按钮或您选择的任何方法来运行 subMakeItSaveable 和 subMakeItUnSaveable,或者您可以在单个例程中实现该功能。 funUpdateCustomDocumentProperty 函数将布尔值写入工作簿自定义属性以切换保存工作表的能力。请注意,这个自定义属性除了提供一个位置来存储不在代码或工作表中的值之外,什么也不做。这提供了一种方便的方法,可以在代码未运行时保存我们的代码所需的数据。

我使用的代码如下:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      ThisWorkbook.Saved = True
    End Sub

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      If ThisWorkbook.CustomDocumentProperties("SaveMyChanges").Value Then
        SaveAsUI = True
        Cancel = False
      Else
        SaveAsUI = False
        Cancel = True
        myTestValue = MsgBox("Read Only Workbook. Save Not Allowed.",         vbInformation, "Operation Aborted")
      End If
    End Sub

    Private Sub Workbook_Open()
      myTestValue = funUpdateCustomDocumentProperty("SaveMyChanges", False, msoPropertyTypeBoolean)
    End Sub

    Public Function funUpdateCustomDocumentProperty(strPropertyName As String, _
     varValue As Variant, docType As Office.MsoDocProperties) As Boolean
    'Function returns true if custom property was added, false if it already exists
    'Originally a sub built by Peter Albert
    'http://stackoverflow.com/users/1867581/peter-albert

      On Error Resume Next
      funUpdateCustomDocumentProperty = False
      ThisWorkbook.CustomDocumentProperties(strPropertyName).Value _
    = varValue

      If Err.Number > 0 Then

        ThisWorkbook.CustomDocumentProperties.Add _
          Name:=strPropertyName, _
          LinkToContent:=False, _
          Type:=docType, _
          Value:=varValue
        funUpdateCustomDocumentProperty = True
      End If
    End Function


    Public Sub subMakeItSaveable()
      myTestValue = funUpdateCustomDocumentProperty("SaveMyChanges", True, msoPropertyTypeBoolean)
    End Sub


    Public Sub subMakeItUnSaveable()
      myTestValue = funUpdateCustomDocumentProperty("SaveMyChanges", False, msoPropertyTypeBoolean)
    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多