c# code use for connect microsoft word..

Recommended Answers

All 8 Replies

do you mean to open Microsoft word through C#?

do you mean to open Microsoft word through C#?

yah exactly! Can you help me about this?

yah sure this is the code
first import these two headers and use them in your header section

using Microsoft.Office.Interop.Word;

using Microsoft.Office;

then write this code in a button click event:

Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();
            wdApp.Visible = true;
            Microsoft.Office.Interop.Word.Document aDoc = wdApp.Documents.Open(@"path where your word document exists");

hope this helps
let me knw if your having any problems

c# code use for connect microsoft word..

Thank you so much....

Check out this code...

///  <summary>

///This method read the document and writes into richtextbox control 

/// </summary>

         private void btn_Read_Click(object sender, EventArgs e)

        {

            // call the method to read ms-word document

            ReadMsWord();

        }

/// <summary>

        /// Read ms-word file

        /// </summary>

        public void ReadMsWord()

        {  
            // variable to store file path

            string filePath = null;

            // open dialog box to select file

            OpenFileDialog file = new OpenFileDialog();

            // dilog box title name

            file.Title = "Word File";

            // set initial directory of computer system

            file.InitialDirectory = "c:\\";

            // set restore directory

            file.RestoreDirectory = true;

            // execute if block when dialog result box click ok button

            if (file.ShowDialog() == DialogResult.OK)

            {

                // store selected file path

                filePath = file.FileName.ToString();          

            }

             try

            { 

                // create word application

                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();

                // create object of missing value

                object miss = System.Reflection.Missing.Value;

                // create object of selected file path

                object path = filePath;

                // set file path mode

                object readOnly = false;

                // open document                

                Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss,                                                                                    ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

              // select whole data from active window document

                docs.ActiveWindow.Selection.WholeStory();

                // handover the data to cllipboard

                docs.ActiveWindow.Selection.Copy();

                // clipboard create reference of idataobject interface which transfer the data

                IDataObject data = Clipboard.GetDataObject();

                //set data into richtextbox control in text format

                richTextBox2.Text = data.GetData(DataFormats.Text).ToString();

                // read bitmap image from clipboard with help of iddataobject interface

                Image img = (Image)data.GetData(DataFormats.Bitmap);

                // close the document

                docs.Close(ref miss, ref miss, ref miss);

            }

            catch (Exception ex) { MessageBox.Show(ex.ToString()); }

   }

It might be resolve your problem.
For more details check [snipped]

merjune- u welcome!!!i am happy u solved it!!!

How are you going to use it?
Is this just a proof-of-concept?

what do u mean???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.