1

我在用户窗体上有一个退出按钮。如果用户单击它,我希望它将 Excel 的设置返回为其原始值,然后关闭工作簿。退出按钮中的代码如下:

Unload Me

If g_Released Then
    ThisWorkbook.Close savechanges:=False
End If

Workbook_BeforeClose 事件中的代码是:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim bSaved As Boolean

bSaved = ThisWorkbook.Saved 
ActiveWindow.DisplayWorkbookTabs = True

'more code here

最后一行不会显示工作簿选项卡。在代码中,我还尝试设置诸如此类的东西Application.DisplayFormulaBar = True,但它们都没有任何影响。似乎这些属性以某种方式被迫进入只读状态,但我不知道为什么。

编辑:这是完整的代码。

Private Sub Workbook_Open()

InitialiseVariables
Application.ScreenUpdating = False
HideExcelUI Application, False, True, False, "Some Company", "Budgeting Module Release 0.1", ThisWorkbook.Path & "\Logo.ico"
HideWorksheetsUI False, False, False
wsBackground.Select

With Application
    .WindowState = xlNormal
    .Height = frmMain.Height
    .Width = frmMain.Width
End With

Application.ScreenUpdating = True
DisplayFormInCenter frmMain

End Sub

Public Sub InitialiseVariables()

g_tDBfolder = ThisWorkbook.Path & "\"
Set g_cn = New ADODB.Connection

With g_cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .Properties("Data Source") = g_tDBfolder & g_tDBname
    .Open
End With

g_ScenarioIsSaved = True
g_ScenarioID = CLng([Scenario_ID])
Set g_rBudgetYear = [BudgetYear]
Set g_rStartMonth = [StartMonth]
Set g_rDealerName = [DealerName]
Set g_rScenario = [Scenario]

End Sub

Public Sub HideExcelUI(ByRef xlApp As Excel.Application, _
                        ByVal ShowFormulaBar As Boolean, ByVal ShowScrollBars As Boolean, ByVal ShowStatusBar As Boolean, _
                        Optional ByVal strApplicationCaption As String, Optional ByVal strWindowCaption As String, Optional ByVal strIcoFile As String)

With xlApp
    .ExecuteExcel4Macro "SHOW.TOOLBAR(""RIBBON"",FALSE)"
    .DisplayFormulaBar = ShowFormulaBar
    .DisplayScrollBars = ShowScrollBars
    .DisplayStatusBar = ShowStatusBar

    If strApplicationCaption <> "" Then
        .Caption = strApplicationCaption
    End If

    If strWindowCaption <> "" Then
        .Windows(1).Caption = strWindowCaption
    End If

    If strIcoFile <> "" Then
        SetIcon strIcoFile, 0
    End If

End With

End Sub

Public Sub HideWorksheetsUI(ByVal ShowGridlines As Boolean, ByVal ShowHeadings As Boolean, ByVal ShowWorkbookTabs As Boolean)

Dim ws As Worksheet, wsCurrent As Worksheet

Application.ScreenUpdating = False
Set wsCurrent = ActiveSheet

For Each ws In ThisWorkbook.Worksheets
    ws.Activate

    With ActiveWindow
        .DisplayGridlines = False
        .DisplayHeadings = False
        .DisplayWorkbookTabs = False
        .Caption = ""
    End With

Next

wsCurrent.Activate
Application.ScreenUpdating = True

End Sub

Public Sub DisplayFormInCenter(ByVal objForm As Object, Optional ByVal bModeless As Boolean)

With objForm
    .startupposition = 0
    .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
    .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)

    If bModeless Then
        .Show vbModeless
    Else
        .Show
    End If

End With

End Sub

Private Sub ExitButton_Click()

Unload Me

If g_Released Then
    ThisWorkbook.Close savechanges:=False
End If

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim bSaved As Boolean, Success As Boolean
Dim UserResponse As Long

bSaved = ThisWorkbook.Saved
ActiveWindow.DisplayWorkbookTabs = True

If g_Released Then

    If Not g_ScenarioIsSaved Then
        UserResponse = MsgBox(Prompt:="There are unsaved changes in the current budget. Save changes?", Buttons:=vbYesNoCancel)

        If UserResponse = vbYes Then
            Success = SaveInputs(ActiveSheet)

            If Not Success Then
                MsgBox "Unexpected error. All inputs were not saved. Please contact vendor."
            End If

        ElseIf UserResponse = vbNo Then
            'Go ahead and close
        Else
            Cancel = True
        End If

    End If

End If

ResetIconToExcel
ThisWorkbook.Saved = bSaved

End Sub
4

0 回答 0