XL2003: Changing ThisWorkbook.Windows().Visible pr

2019-07-17 02:38发布

As title. This is driving me mad and I can't find any reference to it anywhere else on the web.

I have a VBA-laden workbook which is normally hidden through use of:

ThisWorkbook.Windows(1).Visible = False

However, when the workbook is closed I need it to revert to a default state whereby its window is visible and a "special" worksheet which instructs the user on enabling macros is shown. This is to provide guidance should the next user open the workbook with macros disabled. When the workbook is opened with macros enabled, the Workbook_Open event takes care of hiding this message again, at which point normal operation proceeds.

My problem is that when I change the .Windows().Visible property from the workbook's _BeforeClose event (which only occurs whenever Excel is quitting) the workbook simply remains on screen. I have confirmed that it is definitely setting the .Windows().Visible property that is causing this behaviour.

I have tried using the ThisWorkbook.IsAddin property, since this has the same effect of "hiding" the workbook from the user's view, but that causes exactly the same behaviour.

I have also tried simply not changing the window's visibility in any way, in the hope that subsequently opening the workbook with macros disabled will cause the property to be ignored, but this is not the case.

The only thing that I can do to cause the desired behaviour is to have ThisWorkbook.Close as the last line of the _BeforeClose event itself, but this causes instability which manifests as an occasional "Excel has closed unexpectedly, do you want to recover/restart?" message after Excel has quit.

Any help/pointers gratefully accepted. I don't have access to a later version of Excel.

My code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Application.ScreenUpdating = False

    Call cleanUp

    Application.ScreenUpdating = True

End Sub

and

Private Sub cleanUp()

Dim s As Worksheet

    shtZNM.Visible = xlSheetVisible

    For Each s In ThisWorkbook.Sheets
        If Not s Is shtZNM Then s.Visible = xlSheetHidden
    Next s

    With ThisWorkbook.Windows(1)
        .DisplayWorkbookTabs = False
        .DisplayHorizontalScrollBar = False
        .DisplayVerticalScrollBar = False
        .Visible = True '<==REMOVING THIS ALLOWS _BEFORECLOSE TO WORK AS EXPECTED
    End With

    On Error Resume Next
    Application.CommandBars("QCS").Delete
    Err.Clear
    Application.CommandBars("Worksheet Menu Bar").Controls("QCS").Delete

    Me.Save

End Sub

0条回答
登录 后发表回答