In this article, I’ll show you How to Export data into Word Document using C# Windows Form.
And you will find all of the code and output in bottom of article.
Form Design:
1x RichTextBox
1x Button
Step 1: Add a reference by right clicking to the “References” in the Solution Explorer.
Step 2: Then we will see the Reference Manager in the panel. We have to add the Microsoft Word XX.XX Object Library into our project as a reference for exporting to the Microsoft Word.
Step 3: After the adding is finished, let’s start the project. Add the namespace to using
1 2 3 |
using exporttoword = Microsoft.Office.Interop.Word; |
Step 4: Double click to button1 and add following codes.(Button1_Click event).
1 2 3 4 5 6 7 8 9 10 11 12 |
private void button1_Click(object sender, EventArgs e) { exporttoword.Application wordapp = new exporttoword.Application(); wordapp.Visible = true; exporttoword.Document worddoc; object wordobj = System.Reflection.Missing.Value; worddoc = wordapp.Documents.Add(ref wordobj); wordapp.Selection.TypeText(richTextBox1.Text); wordapp = null; } |
Output:
All Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using exporttoword = Microsoft.Office.Interop.Word; namespace export_data_to_word { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { exporttoword.Application wordapp = new exporttoword.Application(); wordapp.Visible = true; exporttoword.Document worddoc; object wordobj = System.Reflection.Missing.Value; worddoc = wordapp.Documents.Add(ref wordobj); wordapp.Selection.TypeText(richTextBox1.Text); wordapp = null; } } } |