简体   繁体   English

如何使用宏重新打开工作簿并取消私有子工作簿_open

[英]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: 我想通过重新打开工作簿来抑制私人子workbook_open:

    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. 但是我找不到正确的vba代码。

Say we have a workbook called yesterday.xlsm which we want to open but we do NOT yesterday 's Open Event macro to trigger. 假设我们有一个名为昨天 .xlsm的工作簿,我们想打开它,但是我们不会触发昨天的 “打开事件”宏。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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