Open save file has old xml sheet names in it and causes a warning?

Jonathan Brotto 1,076 Reputation points
2022-01-24T14:36:39.127+00:00

Open save file has old xml sheet names in it and causes a warning?

I used a code to add sheets and then I rename them, but it does not work. It skips the delete part I wrote.

The part that adds and rename sheets

 Dim addSheets As Excel.Sheets = Globals.ThisAddIn.Application.Sheets

        addSheets.Add()
        addSheets.Add()
        addSheets.Add()

        Dim sheetRename As Excel.Worksheet = Globals.ThisAddIn.Application.Sheets("Sheet1")
        sheetRename.Name = "Raw SAP"
        sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet2")
        sheetRename.Name = "Territory Summary"
        sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet3")
        sheetRename.Name = "Customer Summary"
        sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet4")
        sheetRename.Name = "Product Summary"

        'Sort sheets in correct order
        sheetRename = Globals.ThisAddIn.Application.Sheets("Raw SAP")
        sheetRename.Move(Before:=Globals.ThisAddIn.Application.Worksheets(1))

        sheetRename = Globals.ThisAddIn.Application.Sheets("Customer Summary")
        sheetRename.Move(After:=Globals.ThisAddIn.Application.Worksheets(3))

        sheetRename = Globals.ThisAddIn.Application.Sheets("Product Summary")
        sheetRename.Move(After:=Globals.ThisAddIn.Application.Worksheets(4))

        sheetRename = Globals.ThisAddIn.Application.Sheets("Territory Summary")
        sheetRename.Move(Before:=Globals.ThisAddIn.Application.Worksheets(2))

Delete portion that does not work it seems.

        Try
            sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet2")
            sheetRename.Delete()
            sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet3")
            sheetRename.Delete()
            sheetRename = Globals.ThisAddIn.Application.Sheets("Sheet4")
            sheetRename.Delete()

        Catch ex As Exception

        End Try
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,600 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,574 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 49,166 Reputation points
    2022-01-24T16:15:09.003+00:00

    In your code you have a try-catch around the delete. Most likely it is failing to find the sheets and therefore not deleting them. Put some logic into the catch block to report the error so you can see why it is failing. Ultimately you should debug this by putting a breakpoint on that code and step through it to see exactly what is going wrong.