IRONWORDの使用方法 C#ワード編集(コーディング例 開発者向けチュートリアル) Jordi Bardia 更新日:7月 28, 2025 Download IronWord NuGet Download Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Creating, editing, and managing Word documents is a frequent requirement for many applications. While there are several ways to create and edit a Word document in C#, one of the most powerful methods is using the Microsoft Interop services. With this tool, you can handle Word documents programmatically with much ease. Edit Word and DOCX Documents Download the C# library to edit Word and DOCX documents Create an empty Word document in code Add a new text to the Word document Edit the text in a new or existing document Export the DOCX files to desired location Prerequisites Before setting up the environment and starting with the code, ensure you meet the following prerequisites: Visual Studio: Ensure you have Visual Studio installed on your machine. If not, download and install it from the official Microsoft website. Microsoft Word: Since we're using Microsoft Interop, you should have MS Word installed on your computer. The Interop service interfaces with the Microsoft Word application installed on your machine. Basic C# Knowledge: Understanding basic C# concepts is essential. .NET Framework: Ensure your Visual Studio supports the .NET Framework since our application will be based on it. Setting up the Environment Start by opening the Visual Studio application. Once open, you'll be greeted with a welcome screen. 1. Create a New .NET Framework Console Application Click on "Create a new project". Type "Console App (.NET Framework)" in the search box. From the results, select "Console App (.NET Framework)" and click on the "Next" button. Set a name for your project, then click the "Create" button. After these steps, Visual Studio will generate a new .NET Framework console application for you. In the Program.cs file, you will find a basic template with a Main method, which is the entry point for console applications. 2. Install Microsoft.Office.Interop.Word using NuGet Package Manager NuGet is a package manager for .NET, and it's integrated into Visual Studio. Here's how you can use it to install the Microsoft.Office.Interop.Word package: In Visual Studio, go to the "Tools" menu. Select "NuGet Package Manager" and then "Manage NuGet Packages for Solution...". In the NuGet window, click on the "Browse" tab. In the search box, type Microsoft.Office.Interop.Word and hit enter. From the search results, select the Microsoft.Office.Interop.Word package. On the right side, ensure your console application project is checked, then click on the "Install" button. Visual Studio will now install the package and add a reference to it in your project. This package contains the necessary assemblies and tools to interact with MS Word from your C# application. Introducing IronWord: A Superior Alternative to Interop While Interop provides powerful capabilities for working with Word and Excel, it has limitations. Enter IronWord, a versatile library optimized for .NET developers. IronWord offers a smoother experience than Interop, especially for editing Word document tasks. It not only ensures compatibility and performance but also simplifies complex tasks with intuitive methods. For ease of comparison, I will provide IronWord code snippets for each use case after MS Word, using IronWord version 2024.1.2. Opening an Existing Word Document Often, you may need to edit existing Word documents, the following example shows how to do this in C#: // Create an instance of the Word Application var WordApp = new Microsoft.Office.Interop.Word.Application(); // Open a Word document with the specified file path var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx"); // Create an instance of the Word Application var WordApp = new Microsoft.Office.Interop.Word.Application(); // Open a Word document with the specified file path var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx"); ' Create an instance of the Word Application Dim WordApp = New Microsoft.Office.Interop.Word.Application() ' Open a Word document with the specified file path Dim WordDoc = WordApp.Documents.Open("path_to_your_document.docx") $vbLabelText $csharpLabel In the above code, replace path_to_your_document.docx with the path to your docx file. Using IronWord Open a Word document using IronWord. // Open a Word document with the specified file path using IronWord WordDocument doc = new WordDocument(@"path_to_your_document.docx"); // Open a Word document with the specified file path using IronWord WordDocument doc = new WordDocument(@"path_to_your_document.docx"); ' Open a Word document with the specified file path using IronWord Dim doc As New WordDocument("path_to_your_document.docx") $vbLabelText $csharpLabel Creating a New Word Document To create Word documents from scratch: // Initialize a new instance of the Word Application var WordApp = new Microsoft.Office.Interop.Word.Application(); // Add a new Word document var WordDoc = WordApp.Documents.Add(); // Initialize a new instance of the Word Application var WordApp = new Microsoft.Office.Interop.Word.Application(); // Add a new Word document var WordDoc = WordApp.Documents.Add(); ' Initialize a new instance of the Word Application Dim WordApp = New Microsoft.Office.Interop.Word.Application() ' Add a new Word document Dim WordDoc = WordApp.Documents.Add() $vbLabelText $csharpLabel This code snippet creates a new Word Document you can write and edit using C#. Using IronWord // Create a new, empty Word document using IronWord WordDocument doc = new WordDocument(); // Create a new, empty Word document using IronWord WordDocument doc = new WordDocument(); ' Create a new, empty Word document using IronWord Dim doc As New WordDocument() $vbLabelText $csharpLabel Adding Text to the Word Document To add a new paragraph of text: // Add a new paragraph to the document WordDoc.Paragraphs.Add(); // Assign text to the newly added paragraph WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph."; // Add a new paragraph to the document WordDoc.Paragraphs.Add(); // Assign text to the newly added paragraph WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph."; ' Add a new paragraph to the document WordDoc.Paragraphs.Add() ' Assign text to the newly added paragraph WordDoc.Paragraphs(1).Range.Text = "This is the first paragraph." $vbLabelText $csharpLabel The Paragraphs.Add() method adds a new paragraph to the Word document and the Range.Text property assigns new text to it. Using IronWord // Add a new text to the document using IronWord doc.AddText("Add text using IronWord"); // Add a new text to the document using IronWord doc.AddText("Add text using IronWord"); ' Add a new text to the document using IronWord doc.AddText("Add text using IronWord") $vbLabelText $csharpLabel Editing Existing Text For this tutorial, let's change the first paragraph: // Edit the text of the first paragraph WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph."; // Edit the text of the first paragraph WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph."; ' Edit the text of the first paragraph WordDoc.Paragraphs(1).Range.Text = "This is the edited first paragraph." $vbLabelText $csharpLabel You can also add and edit other elements in the Word document using similar methods. Using IronWord // Edit the text of the first paragraph using IronWord doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph."; // Edit the text of the first paragraph using IronWord doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph."; ' Edit the text of the first paragraph using IronWord doc.Paragraphs(0).TextRuns(0).Text = "This is the edited first paragraph." $vbLabelText $csharpLabel Saving and Closing the Document Once you've made your desired edits: // Save the document to a specified path WordDoc.SaveAs(@"path_where_you_want_to_save.docx"); // Close the document and quit the application WordDoc.Close(); WordApp.Quit(); // Save the document to a specified path WordDoc.SaveAs(@"path_where_you_want_to_save.docx"); // Close the document and quit the application WordDoc.Close(); WordApp.Quit(); ' Save the document to a specified path WordDoc.SaveAs("path_where_you_want_to_save.docx") ' Close the document and quit the application WordDoc.Close() WordApp.Quit() $vbLabelText $csharpLabel Replace path_where_you_want_to_save.docx with your desired path. Using IronWord // Save the document to the desired path using IronWord doc.SaveAs(@"path_where_you_want_to_save.docx"); // Save the document to the desired path using IronWord doc.SaveAs(@"path_where_you_want_to_save.docx"); ' Save the document to the desired path using IronWord doc.SaveAs("path_where_you_want_to_save.docx") $vbLabelText $csharpLabel Complete Code and Example Let's put it all together. Here's a complete code example demonstrating how to open an existing Word document, edit it, and then save the changes: var WordApp = new Microsoft.Office.Interop.Word.Application(); // Create a new Word document var WordDoc = WordApp.Documents.Add(); // Add new text WordDoc.Paragraphs.Add(); WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph."; // Edit the first paragraph WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph."; // Save and close WordDoc.SaveAs(@"path_where_you_want_to_save.docx"); WordDoc.Close(); WordApp.Quit(); var WordApp = new Microsoft.Office.Interop.Word.Application(); // Create a new Word document var WordDoc = WordApp.Documents.Add(); // Add new text WordDoc.Paragraphs.Add(); WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph."; // Edit the first paragraph WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph."; // Save and close WordDoc.SaveAs(@"path_where_you_want_to_save.docx"); WordDoc.Close(); WordApp.Quit(); Dim WordApp = New Microsoft.Office.Interop.Word.Application() ' Create a new Word document Dim WordDoc = WordApp.Documents.Add() ' Add new text WordDoc.Paragraphs.Add() WordDoc.Paragraphs(1).Range.Text = "This is the first paragraph." ' Edit the first paragraph WordDoc.Paragraphs(1).Range.Text = "This is the edited first paragraph." ' Save and close WordDoc.SaveAs("path_where_you_want_to_save.docx") WordDoc.Close() WordApp.Quit() $vbLabelText $csharpLabel Using IronWord The complete code example using IronWord is concise. IronWord utilizes concise code snippets to edit DOCX files. // Create an empty Word document WordDocument doc = new WordDocument(); // Add new text doc.AddText("This is the first paragraph."); // Edit the text doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph."; // Export DOCX doc.SaveAs(@"path_where_you_want_to_save.docx"); // Create an empty Word document WordDocument doc = new WordDocument(); // Add new text doc.AddText("This is the first paragraph."); // Edit the text doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph."; // Export DOCX doc.SaveAs(@"path_where_you_want_to_save.docx"); ' Create an empty Word document Dim doc As New WordDocument() ' Add new text doc.AddText("This is the first paragraph.") ' Edit the text doc.Paragraphs(0).TextRuns(0).Text = "This is the edited first paragraph." ' Export DOCX doc.SaveAs("path_where_you_want_to_save.docx") $vbLabelText $csharpLabel Conclusion In the realm of manipulating Word and Excel documents within .NET applications, choices abound. While Microsoft's Interop services have been a go-to for many, the emergence of solutions like IronWord signifies a shift towards more efficient and user-friendly tools. よくある質問 C#でWord文書を作成および編集するにはどうすればよいですか? C#では、Microsoft InteropサービスまたはIronWordライブラリを使用してWord文書を作成および編集できます。どちらのオプションもWord文書をプログラムで操作できますが、IronWordは性能向上と使いやすさを提供します。 Microsoft InteropよりもWord文書操作でIronWordを使用する利点は何ですか? IronWordは、より洗練された体験を提供し、Word文書編集のための直感的なメソッドを提供することで、Microsoft Interopよりも優れたパフォーマンスを提供します。それは、.NETアプリケーション向けに最適化されており、開発者にとってモダンで効率的な選択肢です。 C#で既存のWord文書を開くにはどうすればよいですか? C#で既存のWord文書を開くには、Microsoft InteropサービスやIronWordのようなライブラリを使用できます。それらは、Wordファイルの内容をプログラムで読み込んで操作するメソッドを提供します。 .NET FrameworkコンソールアプリケーションをWord文書編集のために設定するために必要な手順は何ですか? まず、Visual Studioと.NET Frameworkをインストールしてください。Visual Studioで新しい.NET Frameworkコンソールアプリケーションを作成し、NuGet Package Managerを使用してMicrosoft.Office.Interop.WordパッケージまたはIronWordライブラリをインストールします。 C#でWord文書にテキストを追加するにはどうすればよいですか? Microsoft InteropやIronWordのようなライブラリを使用してWord文書にテキストを追加できます。これらのライブラリは、文書内のテキストの挿入や変更のメソッドを提供します。 C#でWord文書を保存して閉じるにはどうすればよいですか? C#では、Microsoft InteropやIronWordのようなライブラリの提供するメソッドを使用して、Word文書を保存して閉じることができます。これらのメソッドは、変更が保存され、文書が適切に閉じられることを保証します。 Visual StudioでMicrosoft.Office.Interop.Wordパッケージをインストールするプロセスとは? Visual StudioでMicrosoft.Office.Interop.Wordパッケージをインストールするには、「ツール」メニューからNuGet Package Managerにアクセスし、「Manage NuGet Packages for Solution...」を選択し、「Microsoft.Office.Interop.Word」を検索してパッケージをインストールします。 C#でWord文書を編集する際に一般的なエラーをトラブルシューティングするにはどうすればよいですか? C#でWord文書を編集する際の一般的なエラーは、ライブラリの適切なインストールを確認し、.NET Frameworkとの互換性を確認し、文書のパスと権限が正しいことを確認することで解決されることがよくあります。 C#で新しいWord文書を作成するにはどうすればよいですか? C#で新しいWord文書を作成するには、Microsoft InteropやIronWordのようなライブラリを使用できます。これらのライブラリは、新しいWord文書を初期化し、必要に応じてコンテンツを追加するメソッドを提供します。 IronWordを使用してWord文書を編集する完全なコード例はありますか? はい、チュートリアルにはIronWordを使用してWord文書を編集するための完全なコード例が含まれています。Wordアプリケーションインスタンスの作成、テキストの追加と編集、文書の保存が含まれており、IronWordのメソッドの実用的な応用が示されています。 Jordi Bardia 今すぐエンジニアリングチームとチャット ソフトウェアエンジニア Jordiは、最も得意な言語がPython、C#、C++であり、Iron Softwareでそのスキルを発揮していない時は、ゲームプログラミングをしています。製品テスト、製品開発、研究の責任を分担し、Jordiは継続的な製品改善において多大な価値を追加しています。この多様な経験は彼を挑戦させ続け、興味を持たせており、Iron Softwareで働くことの好きな側面の一つだと言います。Jordiはフロリダ州マイアミで育ち、フロリダ大学でコンピュータサイエンスと統計学を学びました。 関連する記事 更新日 9月 18, 2025 ASP.NET Coreでワードファイルをインポート&エクスポートする このガイドでは、既存のワード文書をインポートし、その内容を表示し、IronWordライブラリを使用してスクラッチから文書を作成する方法を探ります。 詳しく読む 更新日 7月 28, 2025 VS 2022 プログラムで新しいワード文書を作成する(チュートリアル) 今日のチュートリアルでは、IronWordを使用してMicrosoft Word文書をプログラムで作成する方法を簡単に説明し、簡単な例を提供します。 詳しく読む 更新日 6月 22, 2025 C#を使用してワードでテキストを整列する方法 IronWord NuGetパッケージに深く掘り下げ、このパッケージを使用してテキストや段落を整列する方法を探ります。 詳しく読む C#でワード文書を作成する方法C#でワードをPDFに変換する...
更新日 9月 18, 2025 ASP.NET Coreでワードファイルをインポート&エクスポートする このガイドでは、既存のワード文書をインポートし、その内容を表示し、IronWordライブラリを使用してスクラッチから文書を作成する方法を探ります。 詳しく読む
更新日 7月 28, 2025 VS 2022 プログラムで新しいワード文書を作成する(チュートリアル) 今日のチュートリアルでは、IronWordを使用してMicrosoft Word文書をプログラムで作成する方法を簡単に説明し、簡単な例を提供します。 詳しく読む
更新日 6月 22, 2025 C#を使用してワードでテキストを整列する方法 IronWord NuGetパッケージに深く掘り下げ、このパッケージを使用してテキストや段落を整列する方法を探ります。 詳しく読む