フッターコンテンツにスキップ
IRONWORDの使用方法

C#ワード編集(コーディング例 開発者向けチュートリアル)

Word 文書の作成、編集、管理は、多くのアプリケーションで頻繁に必要になります。 C# で Word 文書を作成および編集する方法はいくつかありますが、最も強力な方法の 1 つは Microsoft Interop サービスを使用することです。 このツールを使用すると、Word 文書をプログラムで簡単に処理できます。

前提条件

環境を設定してコードを開始する前に、次の前提条件を満たしていることを確認してください。

  1. Visual Studio :お使いのマシンにVisual Studioがインストールされていることを確認してください。インストールされていない場合は、Microsoftの公式ウェブサイトからダウンロードしてインストールしてください。
  2. Microsoft Word : Microsoft Interop を使用するため、コンピューターにMS Wordがインストールされている必要があります。 Interop サービスは、マシンにインストールされている Microsoft Word アプリケーションとインターフェイスします。 3.基本的な C# の知識: 基本的な C# の概念を理解することが不可欠です。
  3. .NET Framework : アプリケーションは .NET Framework をベースにするため、Visual Studio が .NET Framework をサポートしていることを確認してください。

環境のセットアップ

まず、Visual Studio アプリケーションを開きます。 開くと、ようこそ画面が表示されます。

1. 新しい.NET Frameworkコンソールアプリケーションを作成する

1."新しいプロジェクトを作成"をクリックします。

  1. 検索ボックスに"コンソール アプリ (.NET Framework)"と入力します。
  2. 結果から"コンソール アプリ (.NET Framework)"を選択し、"次へ"ボタンをクリックします。
  3. プロジェクトの名前を設定し、次に"作成"ボタンをクリックします。

これらの手順を実行すると、Visual Studio によって新しい .NET Framework コンソール アプリケーションが生成されます。 Program.csファイルには、コンソール アプリケーションのエントリ ポイントであるMainメソッドを含む基本テンプレートがあります。

2. NuGet パッケージ マネージャーを使用してMicrosoft.Office.Interop.Wordをインストールする

NuGet は .NET 用のパッケージ マネージャーであり、Visual Studio に統合されています。 Microsoft.Office.Interop.Wordパッケージをインストールするには、次のようにします。

  1. Visual Studio で、"ツール"メニューに移動します。
  2. "NuGet パッケージ マネージャー"を選択し、"ソリューションの NuGet パッケージの管理..."を選択します。
  3. NuGet ウィンドウで、"参照"タブをクリックします。
  4. 検索ボックスにMicrosoft.Office.Interop.Wordと入力し、Enter キーを押します。
  5. 検索結果から、 Microsoft.Office.Interop.Wordパッケージを選択します。
  6. 右側で、コンソール アプリケーション プロジェクトがチェックされていることを確認し、"インストール"ボタンをクリックします。

! C# 単語編集 (コード例開発者チュートリアル) 図 1

Visual Studio によってパッケージがインストールされ、プロジェクトにパッケージへの参照が追加されます。 このパッケージには、C# アプリケーションからMS Wordと対話するために必要なアセンブリとツールが含まれています。

IronWord のご紹介: Interop の優れた代替手段

Interop は Word や Excel を操作するための強力な機能を提供しますが、制限もあります。 .NET 開発者向けに最適化された多目的ライブラリ、IronWord をご紹介します。 IronWord は、特に Word 文書の編集タスクにおいて、Interop よりもスムーズなエクスペリエンスを提供します。 互換性とパフォーマンスを保証するだけでなく、直感的な方法で複雑なタスクを簡素化します。 比較しやすいように、IronWord バージョン2024.1.2を使用して、 MS Wordに続く各ユースケースの IronWord コード スニペットを提供します。

既存のWord文書を開く

多くの場合、既存の Word 文書を編集する必要がある場合があります。次の例は、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

上記のコードで、 path_to_your_document.docx docx ファイルへのパスに置き換えます。

IronWordの使用

IronWord を使用して Word 文書を開きます。

// 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

新しいWord文書を作成する

Word 文書を最初から作成するには:

// 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

このコード スニペットは、C# を使用して作成および編集できる新しい Word 文書を作成します。

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

Word文書にテキストを追加する

新しいテキストの段落を追加するには:

// 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

Paragraphs.Add()メソッドは Word 文書に新しい段落を追加し、 Range.Textプロパティはそれに新しいテキストを割り当てます。

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

既存のテキストの編集

このチュートリアルでは、最初の段落を変更してみましょう。

// 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

同様の方法を使用して、Word 文書内の他の要素を追加および編集することもできます。

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

ドキュメントを保存して閉じる

必要な編集が完了したら、次の操作を行います。

// 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

path_where_you_want_to_save.docx希望のパスに置き換えます。

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

完全なコードと例

すべてをまとめてみましょう。 既存の Word 文書を開いて編集し、変更を保存する方法を示した完全なコード例を次に示します。

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

IronWordの使用

IronWord を使用した完全なコード例は簡潔です。IronWord は簡潔なコードスニペットを使用して 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
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

結論

.NET アプリケーション内で Word および Excel ドキュメントを操作する場合、選択肢は豊富にあります。 Microsoft の Interop サービスは多くの人にとって頼りになる存在ですが、IronWord のようなソリューションの出現は、より効率的でユーザーフレンドリーなツールへの移行を示しています。

よくある質問

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はフロリダ州マイアミで育ち、フロリダ大学でコンピュータサイエンスと統計学を学びました。