用户在vb.net中的对话框中单击取消后,如何摆脱消息框?

时间:2013-07-31 12:21:04

标签: vb.net

我的表单中有一个SaveAs对话框来保存Excel工作表。当我在对话框中单击取消时,会出现另一个消息框,询问"是否要保存对Book1所做的更改?"。我希望摆脱这个消息框,因为用户已经决定不通过点击取消保存excel ...请帮助..谢谢......

         Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value

    Dim myArray(DataGridView1.Rows.Count - 1, DataGridView1.Columns.Count - 1) As Object

    Dim r As Integer
    Dim c As Integer

    xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = CType(xlWorkBook.Worksheets.Item("sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
                                                                                                                 For r = 0 To DataGridView1.Rows.Count - 1
        For c = 0 To DataGridView1.Columns.Count - 1
            myArray(r, c) = DataGridView1.Rows(r).Cells(c).Value
        Next cc 
       xlWorkSheet.Range("A1").Resize(DataGridView1.Rows.Count, DataGridView1.Columns.Count).Value = myArray
      Dim saveFileDialog As New SaveFileDialog()
    saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
    saveFileDialog.FilterIndex = 2
    saveFileDialog.RestoreDirectory = True

    If saveFileDialog.ShowDialog() = DialogResult.OK Then
        xlWorkSheet.SaveAs(saveFileDialog.FileName)
        MsgBox(" Your file has been saved")

    End If


    xlWorkBook.Close()
    xlApp.Quit()
    xlApp = Nothing
    xlWorkBook = Nothing
    xlWorkSheet = Nothing                                                                                            
 End Sub

2 个答案:

答案 0 :(得分:1)

使用false调用close方法以不显示对话框。

xlWorkBook.Close(False)

More on MSDN

答案 1 :(得分:0)

在退出前尝试此操作:

xlApp.DisplayAlerts = False

或者将False值传递给close方法:

xlWorkBook.Close(False)