How to delete sheet from Excel file without Excel Application Scope

Hello,

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.

image

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

End Try

image

Hi @nilesh.mahajan

Check with the reference links below!

Regards

Hi @nilesh.mahajan ,

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.

Hello, Thnx for reply.

But if I downgrade the package my some activities might not work which I built in current package.

ok got your point.

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.