简体   繁体   中英

Put text to a text box from a word file in c#

I'm working on a project where, i must provide the user a textbox where he can manually enter values or import a word file directly. The project is working fine if the user enters the input manually but fails if a word file is uploaded i used this code for getting text from the word file:

Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                object file = RequirementsFile.Text;// Specify path for word file
                object nullobj = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj,
                                                                                    ref nullobj, ref nullobj, ref nullobj,
                                                                                    ref nullobj, ref nullobj, ref nullobj,
                                                                                    ref nullobj, ref nullobj, ref nullobj,
                                                                                    ref nullobj, ref nullobj, ref nullobj, ref nullobj);
                doc.ActiveWindow.Selection.WholeStory();
                doc.ActiveWindow.Selection.Copy();
                //FileClose(doc);
                doc.Close(ref nullobj, ref nullobj, ref nullobj);
                IDataObject data = Clipboard.GetDataObject();
                string allText = data.GetData(DataFormats.Text).ToString();
                requirements.Text = "";
                requirements.Text += allText;

Once the text is loaded in the text box, if the user edits anything in the text box it is not getting updated.. how to do that..?

The problem is solved..As i'm dealing with textual requirements the format in which the text is uploaded also matters. line breaks and tabs should be handled with care. Used regular expression replacement to handle \\n ,\\t,\\r.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM