Visual Basic - 检查ActiveWorkbook.Save()是否成功

时间:2015-06-23 13:55:04

标签: excel visual-studio-2013 excel-2010 worksheet

如果此保存命令成功,我如何检查?

Dim objExcel As New Microsoft.Office.Interop.Excel.Application

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  objExcel.ActiveWorkbook.Save()
End Sub

例如,当程序在远程Excel工作簿中工作时,连接断开。

1 个答案:

答案 0 :(得分:0)

制定了解决方法。 在更新之前,存储最后一个Excel行号,然后在更新之后,我也存储该号码。

objWorkbook = objExcel.Workbooks.Open("C:\filename.xlsx")
objSheet = objWorkbook.Worksheets("Sheet1")
lRow = objSheet.Cells.Find("*", SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlPrevious).Row + 1
beforeUpdate = lRow

'updating happens
objSheet.Cells(lRow, 1).value = TextBox1.Text

'finding the last available row again
lRow = objSheet.Cells.Find("*", SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlPrevious).Row + 1
afterUpdate = lRow

If beforeUpdate < afterUpdate Then
  MessageBox.Show("Successful update.")
Else
  MessageBox.Show("The update was unsuccessful!")
End If

这种解决方法适合我。