简体   繁体   中英

How is it possible by using a macro to reopen the workbook and suppression of private sub workbook_open

I want reopen a read-only workbook as read and write to change value's with a macro.

I want suppression of the private sub workbook_open by reopen workbook:

    Private Sub Workbook_Open()

    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly

Can anyone help me with this problem?

I know that I have to use a temporary workbook for running the macro for reopening. But I can't find the right vba code.

Say we have a workbook called yesterday.xlsm which we want to open but we do NOT yesterday 's Open Event macro to trigger. Run this in another workbook:

Sub PardonMyParanoia()
   Application.EnableEvents = False
   Workbooks.Open Filename:="C:\TestFolder\yesterday.xlsm"
End Sub

This is NOT tested code but should open a workbook with macros disabled.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM