![](/img/trans.png)
[英]Losing VBA code under 'ThisWorkbook' (Private Sub WorkBook_Open() )
[英]How is it possible by using a macro to reopen the workbook and suppression of private sub workbook_open
我想重新打开一个只读工作簿,以进行读写以通过宏更改值。
我想通过重新打开工作簿来抑制私人子workbook_open:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
谁能帮助我解决这个问题?
我知道我必须使用一个临时工作簿来运行宏才能重新打开。 但是我找不到正确的vba代码。
假设我们有一个名为昨天 .xlsm的工作簿,我们想打开它,但是我们不会触发昨天的 “打开事件”宏。 在另一个工作簿中运行此命令:
Sub PardonMyParanoia()
Application.EnableEvents = False
Workbooks.Open Filename:="C:\TestFolder\yesterday.xlsm"
End Sub
这不是经过测试的代码,但是应该在禁用宏的情况下打开工作簿。
Public Function OpenWorkbookWithMacrosDisabled(ByRef filePathAndName As String, Optional ByRef openReadOnly As Boolean = False) As Boolean
' Stores the current security settings
' sets the security to High, then opens the workbook
' Sets the security settings back to their original settings
' Simon Leten 27-Jun-2014
Dim secAutomation As MsoAutomationSecurity
Dim nameOfFile As String
Dim result As Boolean
' *** Leave errors to get handled by the calling proc ***
' On Error GoTo ErrorHandler
' *** Leave errors to get handled by the calling proc ***
Const PROC_NAME As String = "OpenWorkbookWithMacrosDisabled"
result = False
secAutomation = Application.AutomationSecurity
nameOfFile = Dir$(filePathAndName)
If nameOfFile = "" Then
Err.Raise vbObjectError + 0, PROC_NAME, "Cannot find the file '" & filePathAndName & "'."
Else
If WorkbookIsOpen(nameOfFile) Then
Err.Raise vbObjectError + 0, PROC_NAME, "A file with the name '" & nameOfFile & "' is already open."
Else
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Application.Workbooks.Open Filename:=filePathAndName, ReadOnly:=openReadOnly, AddToMru:=False
result = True
End If
End If
ExitProc:
Application.AutomationSecurity = secAutomation
OpenWorkbookWithMacrosDisabled = result
Exit Function
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.