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

Visual basic - Check if ActiveWorkbook.Save() was successful

如何检查此保存命令是否成功?

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 工作簿中运行时,连接断开。

已解决。 在更新之前,最后一个 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

这个解决方法适合我。