I am trying to Delete Sheet from Excel file and for that I have written VB.Net script, But I am getting error in that script which I am not able to recognize. Can anyone please help to resolve.
Script -
Try
Dim ExcelApp As Microsoft.Office.Interop.Excel.Application
Dim Wb As Microsoft.Office.Interop.Excel.Workbook
Dim Ws As Microsoft.Office.Interop.Excel.Worksheet
ExcelApp = New Microsoft.Office.Interop.Excel.ApplicationClass
Wb= ExcelApp.Workbooks.Open(filename)
ExcelApp.DisplayAlerts = False
TryCast(Wb.Sheets(sheetname),Worksheet).Delete()
ExcelApp.DisplayAlerts = True
Wb.Save
System.Diagnostics.Process.GetProcessesByName("excel")(0).Kill
Catch ex As Exception
This error due to the latest system.activites package. if we downgrade package 20.10.4 resolve the issue. Please have view on the below thread for more details. thanks.
please try the below code and see if you still getting exception.
Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb1 As Microsoft.Office.Interop.Excel.Workbook
Dim ws1 As Microsoft.Office.Interop.Excel.Worksheet
excel = New Microsoft.Office.Interop.Excel.ApplicationClass
wb1 = excel.Workbooks.Open(“Your excel file full path”)
ws1 = CType(wb1.Sheets(“Your sheet Name to delete”), Microsoft.Office.Interop.Excel.Worksheet)
wb1.Application.DisplayAlerts = False
ws1.Delete
wb1.Application.DisplayAlerts = True
wb1.Save
wb1.Close
excel.Quit
ws1 = Nothing
wb1 = Nothing
excel = Nothing
GC.Collect
if still not working getting syntax tree error if you are allowed to use third party packages go for balreva excel package having activity to delete sheet. thanks.