다음을 통해 공유


Workbook.BeforeClose 이벤트(2007 시스템)

업데이트: 2007년 11월

이 이벤트는 통합 문서를 닫기 전에 발생합니다. 통합 문서가 변경된 경우 이 이벤트는 변경 내용을 저장하라는 메시지를 표시하기 전에 발생합니다.

네임스페이스:  Microsoft.Office.Tools.Excel
어셈블리:  Microsoft.Office.Tools.Excel.v9.0(Microsoft.Office.Tools.Excel.v9.0.dll)

구문

Public Event BeforeClose As WorkbookEvents_BeforeCloseEventHandler

Dim instance As Workbook
Dim handler As WorkbookEvents_BeforeCloseEventHandler

AddHandler instance.BeforeClose, handler
public event WorkbookEvents_BeforeCloseEventHandler BeforeClose

예제

다음 코드 예제에서는 BeforeClose 이벤트에 대한 처리기를 보여 줍니다. 이벤트 처리기에서는 통합 문서가 마지막으로 저장된 이후 변경된 내용이 있는 경우 사용자에게 변경 내용을 저장할 것인지, 저장하지 않을 것인지, 닫기 작업을 취소할 것인지를 묻는 메시지를 표시합니다. 사용자가 변경 내용을 저장하지 않으면 통합 문서의 Saved 속성이 true로 설정되어 Microsoft Office Excel에서 닫기 작업을 계속할 때 통합 문서를 저장할 것인지 묻지 않습니다. 사용자가 닫기 작업을 취소하면 WorkbookEvents_BeforeCloseEventHandler 이벤트 처리기의 Cancel 매개 변수가 true로 설정되어 Microsoft Office Excel에서 통합 문서를 닫지 않습니다.

이 예제는 문서 수준 사용자 지정을 위한 것입니다.

Sub ThisWorkbook_BeforeClose(ByRef Cancel As Boolean) _
    Handles Me.BeforeClose

    If Not Me.Saved Then
        Dim result As DialogResult = _
            MessageBox.Show("Do you want to save the " & _
            "changes you made to " & Me.Name & "?", _
            "Example", MessageBoxButtons.YesNoCancel)

        Select Case result
            Case DialogResult.Yes
                Me.Save()
            Case DialogResult.Cancel
                Cancel = True
                ' The following code ensures that the default Save File 
                ' dialog is not displayed.
            Case DialogResult.No
                Me.Saved = True
        End Select
    End If
End Sub
private void WorkbookBeforeClose()
{
    this.BeforeClose +=
        new Excel.WorkbookEvents_BeforeCloseEventHandler(
        ThisWorkbook_BeforeClose);
}

void ThisWorkbook_BeforeClose(ref bool Cancel)
{
    if (!this.Saved)
    {
        DialogResult result = MessageBox.Show("Do you want to save the " +
            "changes you made to " + this.Name + "?", "Example",
            MessageBoxButtons.YesNoCancel);

        switch (result)
        {
            case DialogResult.Yes:
                this.Save();
                break;

            case DialogResult.Cancel:
                Cancel = true;
                break;

            // The following code ensures that the default Save File 
            // dialog is not displayed.
            case DialogResult.No:
                this.Saved = true;
                break;
        }
    }
}

권한

  • 직접 실행 호출자의 경우 완전히 신뢰합니다. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다. 자세한 내용은 부분 신뢰 코드에서 라이브러리 사용을 참조하십시오.

참고 항목

참조

Workbook 클래스

Workbook 멤버

Microsoft.Office.Tools.Excel 네임스페이스