I'm running into an Odd problem that I cant seem to find a solution for. I'm trying to delete any work sheet in a workbook that contains a certain worksheet name and I keep getting this exception: "Unable to get the Delete property of the Worksheet class" I'm able to use the worksheet class in other places but when i try to use the Delete(); method, or even the one that sets the visibility of said sheet I get the same thing. Any help as to why would be appreciated!
Microsoft.Office.Interop.Excel.Sheets TestWorksheets = TestBook.Worksheets;
if (TestWorksheets.Count > NumberofsheetsIWantToKeep)
{
int WorkSheetCounter = TestWorksheets.Count;
while (WorkSheetCounter > NumberofsheetsIWantToKeep)
{
if(TestWorksheets[WorkSheetCounter].Name.Contains("blah"))
{
TestWorksheets[WorkSheetCounter].Delete();
}
WorkSheetCounter--;
}
}