In this example, we’ll learn how to export texbox value to MS Word.
Step 1: Form Design. Add TextBox and Button.
Step 2: Change the MultiLine property of the textBox control.
Step 3: Rigth click on References then click Add Reference.
Step 4: Click COM and Search for Word library. Then use Microsoft Word Object Library ( Maybe version will be changed on your computer)
Step 5: Add those using to alias.
1 2 3 |
using exportWord = Microsoft.Office.Interop.Word; |
Step 6: Button1_Click Code
1 2 3 4 5 6 7 8 9 10 11 12 |
private void button1_Click(object sender, EventArgs e) { exportWord.Application wordapp = new exportWord.Application(); wordapp.Visible = true; exportWord.Document worddoc; object wordobj = System.Reflection.Missing.Value; worddoc = wordapp.Documents.Add(ref wordobj); wordapp.Selection.TypeText(textBox1.Text); wordapp = null; } |
Output:
All of the Codes:
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 35 36 37 38 39 |
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 exportWord = Microsoft.Office.Interop.Word; namespace export_word { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.BackColor = Color.Orange; } private void button1_Click(object sender, EventArgs e) { exportWord.Application wordapp = new exportWord.Application(); wordapp.Visible = true; exportWord.Document worddoc; object wordobj = System.Reflection.Missing.Value; worddoc = wordapp.Documents.Add(ref wordobj); wordapp.Selection.TypeText(textBox1.Text); wordapp = null; } } } |