iOfficeAI/OfficeCLI · error · ArgumentException

Table name '{tableName}' already exists in workbook; choose

Error message

Table name '{tableName}' already exists in workbook; choose a different name.

What it means

Thrown by AddTable's workbook-wide uniqueness check when a table with the same Name (case-insensitive) already exists on any sheet. Excel requires both Name and DisplayName to be unique across the whole workbook; a duplicate surfaces a 'found a problem' repair dialog. The handler walks every WorksheetPart's TableDefinitionParts to enforce this before writing.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs:1108

        // displayName defaults to the (already-sanitized) tableName; if
        // name was user-provided it flows through verbatim so Excel
        // shows the same identifier the user asked for.
        var userProvidedDisplay = properties.ContainsKey("displayName");
        var displayName = SanitizeTableIdentifier(
            properties.GetValueOrDefault("displayName", tableName),
            userProvided: userProvidedDisplay || userProvidedName);

        // CONSISTENCY(table-name-unique): Excel requires both name and
        // displayName to be unique workbook-wide. A duplicate across
        // sheets surfaces a "found a problem" repair dialog. Walk every
        // WorksheetPart's tables, comparing case-insensitively.
        foreach (var existingTable in _doc.WorkbookPart!.WorksheetParts
            .SelectMany(wp => wp.TableDefinitionParts)
            .Select(tdp => tdp.Table)
            .Where(t => t != null)!)
        {
            if (string.Equals(existingTable!.Name?.Value, tableName, StringComparison.OrdinalIgnoreCase))
                throw new ArgumentException(
                    $"Table name '{tableName}' already exists in workbook; choose a different name.");
            if (string.Equals(existingTable.DisplayName?.Value, displayName, StringComparison.OrdinalIgnoreCase))
                throw new ArgumentException(
                    $"Table displayName '{displayName}' already exists in workbook; choose a different displayName.");
        }
        // Excel's name uniqueness spans ListObjects AND workbook defined
        // names in one namespace — a collision passes schema validation but
        // real Excel refuses the file (0x800A03EC).
        var definedNames = _doc.WorkbookPart.Workbook?.DefinedNames;
        if (definedNames != null)
        {
            foreach (var dn in definedNames.Elements<DefinedName>())
            {
                var dnName = dn.Name?.Value;
                if (dnName != null
                    && (string.Equals(dnName, tableName, StringComparison.OrdinalIgnoreCase)
                        || string.Equals(dnName, displayName, StringComparison.OrdinalIgnoreCase)))
                    throw new ArgumentException(

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Choose a different, workbook-unique name.
  2. Remove the existing table first if the name should be reused.
  3. Omit 'name' to let the handler auto-generate a unique TableN name.

Example fix

// before
add /Sheet2/table --prop name=Sales --prop ref=A1:D10
// after
add /Sheet2/table --prop name=Sales2024 --prop ref=A1:D10
Defensive patterns

Strategy: validation

Validate before calling

// Reserve a workbook-unique table name before Add.
static string UniqueTableName(ExcelHandler h, string baseName)
{
    var taken = new HashSet<string>(h.ListTableNames(), StringComparer.OrdinalIgnoreCase); // pseudo
    var name = baseName; int i = 2;
    while (taken.Contains(name)) name = $"{baseName}{i++}";
    return name;
}

Try / catch

try { handler.Add(parentPath, "table", null, props); }
catch (ArgumentException ex) when (ex.Message.Contains("already exists in workbook"))
{ /* generate a unique name and retry */ }

Prevention

When it happens

Trigger: Adding a table whose 'name' property matches an existing table's Name on any sheet, e.g. name=Sales when Sales already exists, including case-only differences (SALES vs sales).

Common situations: Re-running a script without unique names, copying a sheet that already contains the table, or assuming name uniqueness is per-sheet.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/35291828845b9a54. Report an issue: GitHub.