{"record":{"id":"622e70282e26da4d","repo":"iOfficeAI/OfficeCLI","slug":"workbook-not-found","errorCode":null,"errorMessage":"Workbook not found","messagePattern":"Workbook not found","errorType":"exception","errorClass":"System.InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs","lineNumber":1213,"sourceCode":"    }\n\n    private string AddRun(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)\n    {\n        var index = position?.Index;\n        // Add a rich text run to a cell: parentPath = /SheetName/CellRef\n        var runSegments = parentPath.TrimStart('/').Split('/', 2);\n        if (runSegments.Length < 2)\n            throw new ArgumentException(\"Parent path must be /SheetName/CellRef for adding a run\");\n        var runSheetName = runSegments[0];\n        var runCellRef = runSegments[1].ToUpperInvariant();\n        var runWorksheet = FindWorksheet(runSheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {runSheetName}\");\n        var runSheetData = GetSheet(runWorksheet).GetFirstChild<SheetData>()\n            ?? GetSheet(runWorksheet).AppendChild(new SheetData());\n        var runCell = FindOrCreateCell(runSheetData, runCellRef);\n\n        var runWbPart = _doc.WorkbookPart\n            ?? throw new InvalidOperationException(\"Workbook not found\");\n        var runSstPart = runWbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault()\n            ?? runWbPart.AddNewPart<SharedStringTablePart>();\n        SharedStringTable runSst;\n        if (runSstPart.SharedStringTable != null)\n            runSst = runSstPart.SharedStringTable;\n        else\n        {\n            runSst = new SharedStringTable();\n            runSstPart.SharedStringTable = runSst;\n        }\n\n        SharedStringItem? runSsi = null;\n        if (runCell.DataType?.Value == CellValues.SharedString &&\n            int.TryParse(runCell.CellValue?.Text, out var existingSstIdx))\n        {\n            runSsi = runSst.Elements<SharedStringItem>().ElementAtOrDefault(existingSstIdx);\n        }\n        if (runSsi == null)","sourceCodeStart":1195,"sourceCodeEnd":1231,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs#L1195-L1231","documentation":"Thrown by AddRun when _doc.WorkbookPart is null. This is an InvalidOperationException (not ArgumentException) because it indicates the loaded SpreadsheetDocument has no workbook part rather than a bad argument. In practice this means the document was opened in a mode or state where the workbook part is absent or could not be resolved.","triggerScenarios":"The handler was constructed on a document that is not a valid .xlsx (e.g. a flat OPC part stream, a template lacking workbook.xml, or a document opened AutoSave/readonly on a corrupted file). Reachable only after the sheet and cell were found, so a corrupt-but-loadable file can get this far.","commonSituations":"Operating on a .xltm/.xltx template whose workbook part is structured differently. A file that was truncated or partially written. Opening a non-Excel OPC package that the SDK tolerates on load.","solutions":["Re-open the document from a known-good .xlsx copy and retry.","Validate the package before editing: check that the workbook part exists (e.g. via the library's open/validate path).","If the file is a template, save a normal .xlsx copy first and operate on that."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { handler.Add(parentPath, \"run\", pos, props); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Workbook not found\")\n{ /* file is corrupt; re-open from a known-good copy or abort */ }","preventionTips":["Run the library's validate/open check on the file before editing.","Keep a pristine backup copy and re-open from it when a workbook-part error appears.","Avoid editing templates in place; save a normal .xlsx first."],"tags":["excel","ooxml","add-run","workbook-part","invalid-operation","corrupt-file"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}