0

I was trying the below vb.net code to delete some specific sheets from an excel file but it doesn't give me the result what I expected. Actually I need to add that vb.net code into 'Invoke vba' activity in UiPath. Here I have attached all the snapshots of my work. I'm very new to the vb.net and need a clarification for the below code.

Try

app = New Microsoft.Office.Interop.Excel.ApplicationClass
workbook= app.Workbooks.Open(filePath)
TryCast(workbook.Sheets(sheetName),Worksheet).Delete()
workbook.Save
System.Diagnostics.Process.GetProcessesByName("excel")(0).Kill

Catch ex As Exception

End Try

Declared variables, Their types and receptive values

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
Punya Munasinghe
  • 261
  • 1
  • 6
  • 20
  • You said _doesn't give me the result what I expected_. What result are you getting? – JayV Jul 26 '18 at 10:51
  • the empty catch statement could be a first sign of an issue...empty catch statements are a personal pet peeve of mine...that has the information you need to tell you what's wrong – Ctznkane525 Jul 26 '18 at 11:52
  • Agreed. Empty `Catch` blocks should absolutely never be used. Have a look at the top voted answer [here](https://stackoverflow.com/questions/1234343/why-are-empty-catch-blocks-a-bad-idea) – David Wilson Jul 26 '18 at 20:32
  • Do you really need to use VB.NET code? Alternatively, you could just send the appropriate hotkeys to delete the worksheet: https://forum.uipath.com/t/delete-one-sheet-in-excel/22323/3 – Wolfgang Radl Jul 27 '18 at 03:03
  • Please only use tags that are relevant to your question. – Chris Dunaway Jul 27 '18 at 13:35
  • @JayV Actually I wanted to delete some specific sheets from the excel file. But unfortunately it didn't delete sheets as I expected, apart from that it recreated the same sheet again. Now the problem is solved and I have added the corrected code by my self. Thank you so much. – Punya Munasinghe Aug 08 '18 at 05:55

2 Answers2

0

You can create a output of Excel application scope workbook as ExcelWorkbook

Using ExcelWorkbook.GetSheets you get the sheets, iterate it with for each loop as foreach item in ExcelWorkbook.GetSheets

now you have a every single worksheet name you can perform delete operation or also skip it

Thanks!! Might it help you

Abhishek Vaidya
  • 102
  • 2
  • 10
0

Thank you so much for all your answers. Here I've solved it myself with including two small code lines as follows.

Try

    app = New Microsoft.Office.Interop.Excel.ApplicationClass
    workbook= app.Workbooks.Open(filePath)
    app.DisplayAlerts = False
    TryCast(workbook.Sheets(sheetName),Worksheet).Delete()
    app.DisplayAlerts = True
    workbook.Save
    System.Diagnostics.Process.GetProcessesByName("excel")(0).Kill

    Catch ex As Exception

End Try
Punya Munasinghe
  • 261
  • 1
  • 6
  • 20