JFToolkit.WordToCUpdater 0.4.1

dotnet add package JFToolkit.WordToCUpdater --version 0.4.1
                    
NuGet\Install-Package JFToolkit.WordToCUpdater -Version 0.4.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JFToolkit.WordToCUpdater" Version="0.4.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JFToolkit.WordToCUpdater" Version="0.4.1" />
                    
Directory.Packages.props
<PackageReference Include="JFToolkit.WordToCUpdater" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JFToolkit.WordToCUpdater --version 0.4.1
                    
#r "nuget: JFToolkit.WordToCUpdater, 0.4.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package JFToolkit.WordToCUpdater@0.4.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JFToolkit.WordToCUpdater&version=0.4.1
                    
Install as a Cake Addin
#tool nuget:?package=JFToolkit.WordToCUpdater&version=0.4.1
                    
Install as a Cake Tool

JFToolkit.WordToCUpdater

Zero NuGet dependencies. Updates Word Table of Contents page numbers. Works with Word 2007 through Office 365. No version coupling.

NuGet License Target

The problem

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:

  1. Open Word
  2. Right-click the TOC
  3. Click "Update Field"
  4. Choose "Update page numbers only"
  5. 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.

Install

dotnet add package JFToolkit.WordToCUpdater

No NuGet dependencies. No Office PIA references. No SDK requirements beyond .NET 8+.

Usage

One-liner

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}");

With options

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}");

Async queue (update many documents without blocking)

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");

Check if Word is installed

var version = TocUpdater.DetectVersion();
// "Office365", "Word2016", "Word2013", "NotInstalled", etc.

How it works

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).

Requirements

  • Windows (Word COM is Windows-only)
  • Microsoft Word installed (any version 2007+)
  • .NET 8 or .NET 9

Error cases handled

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

Version compatibility

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.

What it does NOT do

  • 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)

Building from source

git clone https://github.com/you/JFToolkit.WordToCUpdater.git
cd JFToolkit.WordToCUpdater
dotnet build
dotnet test

License

MIT — use it anywhere, commercial or personal.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.1 97 6/12/2026
0.4.0 92 6/12/2026
0.3.0 103 6/5/2026
0.2.0 108 6/4/2026
0.1.0 111 6/3/2026