IRONWORDの使用方法 C#ワード印刷チュートリアル:ステップバイステップガイド Jordi Bardia 更新日:6月 22, 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 このチュートリアルへようこそ。ここでは、Microsoft Interopを使用してC#コンソールアプリケーションでWordドキュメントを印刷する方法を探ります。 この初心者向けガイドは、Microsoft Wordドキュメントをプログラムで印刷する手順を案内します。 前提条件 コードに入る前に、いくつかの準備をしておくことが重要です: Microsoft Wordインストール:システムにMicrosoft Wordがインストールされていることを確認してください。 インストールされていない場合は、公式のMicrosoftウェブサイトまたはアプリストアからインストールしてください。 Visual Studioセットアップ:コンソールアプリケーションを作成できるVisual Studioがインストールされている必要があります。 初心者の場合、無料で必要十分な Visual Studio Community をダウンロードすることを検討してください。 Wordドキュメント:テスト目的でサンプルのWordドキュメントを用意してください。 これがプリンターに送信するドキュメントになります。 環境の設定 新しいコンソールアプリケーションを作成する Visual Studioを開きます。 「Create a new project」をクリックします。 「Console App」を検索し、適切なC#テンプレートを選択します。 プロジェクトに名前を付け(例:「InteropPrintConsoleTutorial」)、適切な場所を選択します。 Interop参照を追加する Interopを使用するには、Microsoft Office Interopライブラリへの参照が必要です。 追加方法は次のとおりです: Visual Studioで、ソリューションエクスプローラーのコンソールプロジェクトを右クリックします。 Add > Referenceに移動します。 Reference Managerウィンドウで、COMタブに移動します。 検索バーに「Microsoft Word」と入力してリストを絞り込みます。 結果から、「Microsoft Word xx.x Object Library」(xx.xはバージョン番号)を選択します。 OKボタンをクリックして参照を追加します。 NuGetパッケージマネージャを使用してもインストールできます。 アプリケーション設定を確認する アプリケーションのターゲットフレームワークがInteropライブラリと互換性があることを確認してください。 これは、ソリューションエクスプローラーでプロジェクトを右クリックし、プロパティを選択し、アプリケーションタブのターゲットフレームワークを表示することで確認できます。 Interopライブラリのバージョンに問題がある場合は、必要なパッケージやアセンブリをダウンロードするか、対象フレームワークのバージョンを調整することを検討してください。 環境が設定されたので、コーディングプロセスに進むことができます。 ドキュメントオブジェクトの理解 ドキュメントオブジェクトは、Wordドキュメントを扱う際にInteropサービスの中心にあります。 このオブジェクトはMicrosoft Wordドキュメントを表し、すべての機能を提供します。 一般的なタスクはドキュメントを開くことです: using Word = Microsoft.Office.Interop.Word; // Object needed to avoid passing specific parameters object oMissing = Type.Missing; // File path to the Word document you want to open object fileName = @"C:\path_to_document\document.docx"; // Create a new instance of the Word application Word.Application wordApp = new Word.Application(); // Open the document with specified parameters Word._Document wordDoc = wordApp.Documents.Open( ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); using Word = Microsoft.Office.Interop.Word; // Object needed to avoid passing specific parameters object oMissing = Type.Missing; // File path to the Word document you want to open object fileName = @"C:\path_to_document\document.docx"; // Create a new instance of the Word application Word.Application wordApp = new Word.Application(); // Open the document with specified parameters Word._Document wordDoc = wordApp.Documents.Open( ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); Imports Word = Microsoft.Office.Interop.Word ' Object needed to avoid passing specific parameters Private oMissing As Object = Type.Missing ' File path to the Word document you want to open Private fileName As Object = "C:\path_to_document\document.docx" ' Create a new instance of the Word application Private wordApp As New Word.Application() ' Open the document with specified parameters Private wordDoc As Word._Document = wordApp.Documents.Open(fileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing) $vbLabelText $csharpLabel 複数のパラメータ ref oMissing が見かけ上圧倒されるかもしれませんが、多くの引数を必要とするOpenメソッドにとって重要です。ほとんどはオプションです。 印刷機能の実装 環境を設定し、ドキュメントオブジェクトを理解したので、Wordドキュメントを印刷する核心の機能に入りましょう。 Wordドキュメントの基本的な印刷 ドキュメントを印刷するには、次のメソッドを使用できます: // Method to print the document using default printer settings private void ButtonPrint_Click(object sender, EventArgs e) { wordDoc.PrintOut(); // Sends the document to the default printer } // Method to print the document using default printer settings private void ButtonPrint_Click(object sender, EventArgs e) { wordDoc.PrintOut(); // Sends the document to the default printer } ' Method to print the document using default printer settings Private Sub ButtonPrint_Click(ByVal sender As Object, ByVal e As EventArgs) wordDoc.PrintOut() ' Sends the document to the default printer End Sub $vbLabelText $csharpLabel このメソッドは、デフォルト設定を使用してドキュメントをデフォルトのプリンターに送信します。 カスタマイズ付きのWordドキュメントの印刷 印刷ダイアログを導入したり、プリンター設定をカスタマイズしたり、複数のページを印刷したりする場合は、より詳細なアプローチが必要です: // Method to print the document with custom settings private void ButtonPrintWithSettings_Click(object sender, EventArgs e) { // Number of copies to print object copies = "1"; // Page range to print, e.g., pages 1 to 3 object pages = "1-3"; // Print the document with specified copies and page range wordDoc.PrintOut(Copies: ref copies, Pages: ref pages); } // Method to print the document with custom settings private void ButtonPrintWithSettings_Click(object sender, EventArgs e) { // Number of copies to print object copies = "1"; // Page range to print, e.g., pages 1 to 3 object pages = "1-3"; // Print the document with specified copies and page range wordDoc.PrintOut(Copies: ref copies, Pages: ref pages); } ' Method to print the document with custom settings Private Sub ButtonPrintWithSettings_Click(ByVal sender As Object, ByVal e As EventArgs) ' Number of copies to print Dim copies As Object = "1" ' Page range to print, e.g., pages 1 to 3 Dim pages As Object = "1-3" ' Print the document with specified copies and page range wordDoc.PrintOut(Copies:= copies, Pages:= pages) End Sub $vbLabelText $csharpLabel 上記のソースコードでは、ページ範囲とコピー数を指定しますが、カスタマイズの可能性は無限大です。 印刷設定のカスタマイズ 印刷設定を変更できることは、プログラム制御の特徴です。 プリンター設定を調整したり、特定のプリンターを定義したり、ドキュメントを静かに印刷したりすることも、Interopで可能です。 静かに印刷 ユーザーの操作なしにドキュメントをプリンターに送信するのが静かな印刷です: // Object to determine whether to print in the background or not object background = false; // Print the document silently (no user interactions) wordDoc.PrintOut(Background: ref background); // Object to determine whether to print in the background or not object background = false; // Print the document silently (no user interactions) wordDoc.PrintOut(Background: ref background); ' Object to determine whether to print in the background or not Dim background As Object = False ' Print the document silently (no user interactions) wordDoc.PrintOut(Background:= background) $vbLabelText $csharpLabel プリンターを指定する デフォルト以外の特定のプリンターでドキュメントを印刷するには: // Set the active printer to a specified printer by name wordApp.ActivePrinter = "Printer Name"; // Print the document using the specified printer wordDoc.PrintOut(); // Set the active printer to a specified printer by name wordApp.ActivePrinter = "Printer Name"; // Print the document using the specified printer wordDoc.PrintOut(); ' Set the active printer to a specified printer by name wordApp.ActivePrinter = "Printer Name" ' Print the document using the specified printer wordDoc.PrintOut() $vbLabelText $csharpLabel 高度なプリンター設定 プリンターの指定以外にも、プリンター設定を調整する必要がある場合があります: // Creates a PrintDialog to allow the user to choose printer settings PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == DialogResult.OK) { // Sets the Word application's active printer to the user's choice wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName; // Prints the document using user's selected printer settings wordDoc.PrintOut(); } // Creates a PrintDialog to allow the user to choose printer settings PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == DialogResult.OK) { // Sets the Word application's active printer to the user's choice wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName; // Prints the document using user's selected printer settings wordDoc.PrintOut(); } ' Creates a PrintDialog to allow the user to choose printer settings Dim printDialog As New PrintDialog() If printDialog.ShowDialog() = DialogResult.OK Then ' Sets the Word application's active printer to the user's choice wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName ' Prints the document using user's selected printer settings wordDoc.PrintOut() End If $vbLabelText $csharpLabel このようにして、ユーザーは手動で設定(向き、両面印刷など)を調整できます。 IronWordの紹介 Microsoft InteropはWordドキュメントを管理するための機能を提供しますが、商業用途においては堅固で効率的であるべきものではありません。 IronWordに登場—Word DOCXファイル処理のためのInteropに代わる優れた選択肢。 IronWordはC#でのExcelファイルのシームレスな読み取り、書き込み、操作を可能にします。 IronWordを始める方法についてさらに学びます here。 結論 このチュートリアルでは、Microsoft Interopを活用してC#コンソールアプリケーションでWordドキュメントをプログラムで印刷する手順を掘り下げました。 印刷ダイアログを表示し、カスタム印刷設定を設定し、特定のプリンターの選択やページ範囲の定義など、さまざまな印刷の側面を制御する方法を見てきました。 Interopは基礎的な機能を提供しますが、IronWordのような強力な代替品があることに注意する価値があります。 よくある質問 C#でWordドキュメントを印刷するための前提条件は何ですか? C#でWordドキュメントを印刷するには、Microsoft WordとVisual Studioがマシンにインストールされている必要があります。代わりに、IronWordを使用してドキュメントを処理できますが、Microsoft Wordをインストールする必要はありません。 C#でWordドキュメントを印刷するためにVisual Studioで新しいコンソールアプリケーションをどのようにセットアップできますか? Visual Studioで新しいコンソールアプリケーションをセットアップするには、IDEを開き、「新しいプロジェクトを作成」を選択し、『Console App』を検索して、C#テンプレートを選んでプロジェクトに適切な名前を付けます。 Wordドキュメント印刷のためにMicrosoft Interopライブラリへの参照をどのように追加しますか? Visual Studioで、プロジェクトを右クリックし、『Add > Reference』を選択し、COMタブの下で『Microsoft Word xx.x Object Library』を選択します。IronWordを使用すると、COM参照を必要とせずにWordドキュメントを管理できます。 Word InteropサービスにおけるDocument Objectの役割は何ですか? InteropサービスのDocument ObjectはMicrosoft Wordドキュメントを表しており、プログラムによるドキュメントの操作を可能にします。IronWordは、パフォーマンスと効率が向上した類似の能力を提供します。 C#で既定のプリンター設定を使用してWordドキュメントをどのように印刷しますか? InteropでwordDoc.PrintOut()メソッドを使用して、既定のプリンター設定でWordドキュメントを印刷できます。IronWordは、設定に対してさらなる制御を持つ簡素化された印刷プロセスを提供します。 C#でWordドキュメントの印刷設定をカスタマイズするためにどのステップが含まれていますか? コピー数やページ範囲などの印刷設定をカスタマイズするには、指定されたパラメーターCopies: ref copiesとPages: ref pagesを使用してPrintOutメソッドを使用します。IronWordはカスタマイズ可能な印刷のための類似のオプションを提供します。 C#でのWordドキュメントのサイレント印刷はどのように機能しますか? サイレント印刷は、wordDoc.PrintOut(Background: ref background)メソッドのBackgroundパラメーターをfalseに設定することで、ユーザーの操作なしにドキュメントを印刷できるようにします。IronWordは効率的にサイレント印刷をサポートします。 C#でWordドキュメントを印刷するために既定以外のプリンターをどのように選択しますか? wordApp.ActivePrinterを実行する前に、希望するプリンターの名前に設定してwordDoc.PrintOut()をおこなうことで、別のプリンターを指定できます。IronWordはプリンターの選択のための同様の機能を有効にします。 C#でWordドキュメントを処理するためにIronWordを使用する利点は何ですか? IronWordは、Microsoft Wordのインストールを必要とせずに、C#でDOCXファイルのシームレスな読み取り、書き込み、および操作を可能にする、強力で効率的なWordドキュメント処理を提供します。 C#でWordドキュメントを印刷するときにカスタマイズのために印刷ダイアログをどのように導入しますか? 印刷ダイアログを導入するには、PrintDialogクラスを使用してユーザーにプリンター設定を選択させ、その後にwordApp.ActivePrinterを選択したプリンタの名前に設定して印刷を行います。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#オープンワード文書
更新日 9月 18, 2025 ASP.NET Coreでワードファイルをインポート&エクスポートする このガイドでは、既存のワード文書をインポートし、その内容を表示し、IronWordライブラリを使用してスクラッチから文書を作成する方法を探ります。 詳しく読む
更新日 7月 28, 2025 VS 2022 プログラムで新しいワード文書を作成する(チュートリアル) 今日のチュートリアルでは、IronWordを使用してMicrosoft Word文書をプログラムで作成する方法を簡単に説明し、簡単な例を提供します。 詳しく読む
更新日 6月 22, 2025 C#を使用してワードでテキストを整列する方法 IronWord NuGetパッケージに深く掘り下げ、このパッケージを使用してテキストや段落を整列する方法を探ります。 詳しく読む