Zero NuGet dependencies. Updates Word Table of Contents page numbers. Works with Word 2007 through Office 365. No version coupling.
You have a Word document with a table of contents. You add 20 pages of content. The page numbers in the TOC are now wrong. You have to:
- Open Word
- Right-click the TOC
- Click "Update Field"
- Choose "Update page numbers only"
- Save and close
Now imagine doing this for 50 documents. Or doing it from a C# service that generates Word reports. This library makes it one line of code.
dotnet add package JFToolkit.WordToCUpdaterNo NuGet dependencies. No Office PIA references. No SDK requirements beyond .NET 8+.
using JFToolkit.WordToCUpdater;
var result = TocUpdater.Update(@"C:\Reports\Q2-Report.docx");
Console.WriteLine(result.Success
? $"Done. {result.TocsUpdated} TOCs updated. {result.Elapsed.TotalSeconds:F1}s"
: $"Failed: {result.Error}");var result = TocUpdater.Update(@"C:\Reports\spec.docx", new TocUpdateOptions
{
UpdatePageNumbersOnly = false, // full rebuild — use if headings changed
CreateBackup = true, // leaves spec.docx.bak
OutputPath = @"C:\Reports\spec_final.docx" // don't touch original
});
Console.WriteLine($"Pages: {result.PagesBefore} → {result.PagesAfter}");
Console.WriteLine($"Word version: {result.WordVersion}");await using var queue = new TocUpdateQueue();
var tasks = Directory.GetFiles(@"C:\Reports", "*.docx")
.Select(path => queue.EnqueueAsync(path));
var results = await Task.WhenAll(tasks);
foreach (var r in results)
Console.WriteLine($"{r.OutputPath}: {(r.Success ? "✓" : "✗")} {r.Elapsed.TotalSeconds:F1}s");var version = TocUpdater.DetectVersion();
// "Office365", "Word2016", "Word2013", "NotInstalled", etc.Late-bound COM interop via dynamic. No compile-time reference to any Word
interop assembly — the same binary works with Word 2007, 2010, 2013, 2016,
2019, and Office 365.
The TocUpdateQueue spins up a dedicated STA thread with a Windows message
pump — COM requires this. Your code stays async, the COM calls happen on the
right thread, and Word.Application is created and destroyed cleanly (no zombie
WINWORD.EXE processes).
- Windows (Word COM is Windows-only)
- Microsoft Word installed (any version 2007+)
- .NET 8 or .NET 9
| Scenario | Behaviour |
|---|---|
| Word not installed | Returns Success=false with clear message |
| File not found | Returns Success=false before touching COM |
| File locked by another process | COM error caught, reported in Error |
| Document has no TOC | Returns TocsUpdated=0, Success=true (no-op) |
| Word.exe hangs | Dispose force-quits after 5s timeout on the STA thread |
| Multiple concurrent updates | Queued sequentially on one STA thread — no conflicts |
Late binding means this library ships zero interop assemblies. It queries
Type.GetTypeFromProgID("Word.Application") at runtime. The COM API for
TablesOfContents.Update() has been stable since Word 97. This library
targets the intersection that works across all versions.
The WordVersion enum is informational only — no behaviour branches on version.
- Does not create or format TOC fields (use Open XML SDK for document creation)
- Does not render Word → PDF
- Does not run on Linux/macOS (Word COM is Windows-only)
- Does not handle password-protected documents (throws immediately with clear error)
git clone https://github.com/you/JFToolkit.WordToCUpdater.git
cd JFToolkit.WordToCUpdater
dotnet build
dotnet testMIT — use it anywhere, commercial or personal.