You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Microsoft.Office.Interop.Word编程移除Word文档行号?

Removing All Line Numbers from a Word Document via Microsoft.Office.Interop.Word

Absolutely, this is totally feasible—and yes, the LineNumbering interface is exactly the right tool for the job! Word’s line numbering is configured per section, so you’ll need to target every section in your document to fully wipe out line numbers. Here’s a detailed breakdown of how to make it happen:

Key Context to Know

Word applies line number settings at the section level, not across the entire document in one go. That means even if your document looks like a single continuous block, hidden section breaks (like those after page breaks or custom headers/footers) will hold their own line number configurations. The LineNumbering property of a Section object controls all line number behaviors for that specific section.

Step-by-Step Implementation (C# Example)

First, ensure you’ve added a reference to Microsoft.Office.Interop.Word in your project—you can do this via NuGet or by adding the COM reference directly in Visual Studio.

Here’s a complete, clean code snippet to open a document, strip all line numbers, save changes, and avoid memory leaks:

using Microsoft.Office.Interop.Word;
using System;

class WordLineNumberCleanup
{
    static void Main(string[] args)
    {
        Application wordApp = null;
        Document targetDoc = null;

        try
        {
            // Initialize Word application in background mode
            wordApp = new Application();
            wordApp.Visible = false;

            // Path to your Word document
            string docPath = @"C:\Your\File\Path\document.docx";
            targetDoc = wordApp.Documents.Open(docPath);

            // Loop through every section to disable line numbering
            foreach (Section section in targetDoc.Sections)
            {
                section.LineNumbering.Active = false;
            }

            // Save changes and close the document
            targetDoc.Save();
            targetDoc.Close();

            Console.WriteLine("All line numbers removed successfully!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Oops, something went wrong: {ex.Message}");
        }
        finally
        {
            // Clean up COM objects to prevent lingering Word processes
            if (targetDoc != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(targetDoc);
            if (wordApp != null)
            {
                wordApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
            }
        }
    }
}

Extra Tips

  • If you only need to remove line numbers from specific sections, adjust the loop to target those sections (e.g., targetDoc.Sections[2] for the third section—note Word uses 1-based indexing).
  • Setting LineNumbering.Active = false completely turns off all line numbering for the section, overriding any existing settings like start values, increment counts, or spacing from text.
  • Never skip the COM object cleanup step—unclosed Word processes can hang around in your system memory otherwise.

内容的提问来源于stack exchange,提问作者Renaud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:46:59