C # reading A Word document instance analysis

Source: Internet
Author: User

How does C # read a Word document? We can use the FileStream object to read the information in the text file, but we can't use this method for Word documents.

In this case, C # to read the implementation of the Word document we need to use the "Microsoft Word 9.0 Object Library" COM component, which provides us with all the objects and methods used to read Word documents.

Here we mainly use the Word.applicationclass method to deal with word applications. The idea is to open the Word document in memory first, and then put the contents of the inside of the entire copy of the Clipboard, and finally the data from the Clipboard to take out.

C # reads the Word document instance code as follows:

Word.ApplicationClass wordApp=new ApplicationClass();

object file=path;

object nullobj=System.Reflection.Missing.Value;

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);

doc.ActiveWindow.Selection.WholeStory();

doc.ActiveWindow.Selection.Copy();

IDataObject data=Clipboard.GetDataObject();

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

doc.Close();

C # to read the relevant content of the Word document to you here, I hope that you understand and learn C # reading Word documents are helpful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.