【发布时间】:2014-10-07 08:59:43
【问题描述】:
我有一个相当大的工作簿,其中包含一个非常复杂的 VBA 项目。
工作簿还有一个自定义功能区...
Workbook_Open 事件中也发生了很多事情,取消保护和重新保护 (userInterfaceOnly)、隐藏和显示各种工作表、存储对功能区的引用等。
当宏没有自动启用时,工作簿打开,启用按钮出现,当我点击它时,一切都会发生。
当文件受信任并且宏自动运行时会出现问题。在这些情况下,它倾向于使应用程序崩溃。
就好像,如果在用户必须单击按钮的地方出现暂停,它会执行所有 Excel 准备工作,显示功能区,然后执行我所有的 Workbook_Open 内容,但是如果没有启用按钮,Excel 会以某种方式自己启动东西和显示功能区似乎与我的 Workbook_Open 事件纠缠在一起,导致它崩溃。
这发生在 2007 年、2010 年和 2013 年以及 Windows 7 和 8(.1) 上
我怀疑这与功能区有关,因为有时问题不是完全崩溃,而是工作簿打开,功能区区域只是空白。
仅供参考,Workbook_Open 事件中的代码如下所示:
enter code here
Private Sub Workbook_Open()
Dim newHour As Single
Dim newMinute As Single
Dim newSecond As Single
Dim waitTime As Date
Application.ScreenUpdating = False
' To activate the ribbon in v2007
Application.SendKeys "%Q{RETURN}"
Call ResetTheSheets
Splash.Visible = xlSheetVisible
Splash.Protect shtHidden.Range("iWord")
Call ShowHideSplash
ExecSumm.Visible = xlSheetVisible
GetStarted.Visible = xlSheetVisible
' Look at the properties in the Prop sheet to see what should be displayed
If Prop.Range("PropHideGetStarted") Then
On Error Resume Next
With ExecSumm
.Activate
'Range("C4").Select
.Unprotect Password:=shtHidden.[iWord]
.Visible = xlSheetVisible
.Range("A1:A" & .Range("LastRowCol").Row).EntireRow.Hidden = False
.Range(.Cells(1, 1), .Cells(1, .Range("LastRowCol").Column)).EntireColumn.Hidden = False
' Check to see if the template column has anything in
If WorksheetFunction.CountA(.Range("TemplateCol")) = 0 Then .Range("TemplateCol").EntireColumn.Hidden = True
' Check to see if the prior year should be hidden
If Not Prop.Range("PropBFbalances") Then .Range("ppTitle").EntireColumn.Hidden = True
.Activate
End With
Else
GetStarted.Select
End If
ActiveWindow.ScrollRow = 1
FormShow:
Err.Clear
On Error GoTo FormShow
frmSplash.Show msoFalse
shtHidden.Range("iBalance") = "TRUE"
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'' Schedule the start to avoid 'startup-crashes' - this didn't work as a way to prevent
' newHour = Hour(Now())
' newMinute = Minute(Now())
' newSecond = Second(Now()) + 1
' waitTime = TimeSerial(newHour, newMinute, newSecond)
' Application.Wait waitTime
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Err.Clear
On Error GoTo 0
Application.OnTime Now + TimeValue("00:00:01"), "StartUpMacro"
End Sub
【问题讨论】:
-
您是否尝试过将所有您的 Open 代码移至 StartUpMacro 例程?