簡體   English   中英

如何使用宏重新打開工作簿並取消私有子工作簿_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM