C# windows application code to open word document

.doc , .docx

namespace : using Microsoft.Office.Interop.Word

This simple c# windows application code shows how to open existing word document programmatically by passing password. First step add Microsoft.Office.Interop.Word in reference. Get the document path from the user By openFileDialog method with Filter open only word document with specific extension .doc , .docx.

csharp open word code

Open existing Word document


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace Word_Password
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string _szFilePath = "";

            Word._Application objApp = null;
            Word._Document objDoc = null;
            try
            {
                object objMiss = System.Reflection.Missing.Value;
                object fileToOpen = _szFilePath;
                object szPassword = "Pass";
                object bFalse = false;
                object bTrue = true;
                //Create application object and open existing document
                objApp = new Word.Application();
                objDoc = objApp.Documents.Open(ref fileToOpen, ref objMiss, ref objMiss, ref objMiss, ref objMiss,
                                                ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss,
                                                ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);

                if (objDoc.ProtectionType != Word.WdProtectionType.wdNoProtection)
                {
                    objDoc.Unprotect(ref szPassword);
                    objDoc.Save();
                    MessageBox.Show("Word document UnProtected successfully !", "Word Protect", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                { 
                    MessageBox.Show("Selected word document is not protected !", "Word Protect", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (objDoc != null)
                {
                    objDoc.Close();
                    objDoc = null;
                }
                if (objApp != null)
                {
                    objApp.Quit();
                    objApp = null;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                InitialDirectory = @"D:\",
                Title = "Browse Text Files",
                CheckFileExists = true,
                CheckPathExists = true,
                DefaultExt = "Word",
                Filter = "Word files (*.doc)|*.docx",
                FilterIndex = 2,
                RestoreDirectory = true,
                ReadOnlyChecked = true,
                ShowReadOnly = true
            };
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName;
            }
        }
    }
}