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

162 views Asked by At

How can i check, if this saving command 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

For example, while the program is working in a remote excel workbook, and the connection goes down.

1

There are 1 answers

0
Gabor On BEST ANSWER

Made a workaround. Before update, the last Excel row number is stored, then after update, i store that number too.

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

This workaround makes it for me.