Compatible Word with traditional reading methods

Source: Internet
Author: User
# Region traditional reading Method

Object readonly = true;
Object missing = system. reflection. Missing. value;
Object filename = file. fullname;

Microsoft. Office. InterOP. Word. applicationclass wordapp = new applicationclass ();

// Open the specified file (the number of COM parameters varies in different versions. In general, all except the first one will use missing)
Microsoft. Office. InterOP. Word. Document Doc = wordapp. Documents. Open (ref filename, ref missing, ref readonly, ref missing,
Ref missing, ref missing,
Ref missing, ref missing,
Ref missing, ref missing );
// Obtain the text in the DOC file
String text = Doc. content. text;
// Close the file
Doc. Close (ref missing, ref missing, ref missing );
// Close com
Wordapp. Quit (ref missing, ref missing, ref missing );


# Endregion


// The word is referenced statically in the program. If it is not installed, an error is returned. A program such as this requires that the corresponding version of the program be installed.
// You can use dynamic references to check whether a program is installed or whether the version is compatible.
# Region reflection read


Type objwordtype = type. gettypefromprogid ("Microsoft. Office. InterOP. Word ");

Object objwordapp = activator. createinstance (objwordtype );
Object objdocuments = objwordapp. GetType (). invokemember ("documents", bindingflags. getproperty, null, objwordapp, null );
Object [] openparas = {file. fullname };
Object OpenDocument = objdocuments. GetType (). invokemember ("open", bindingflags. invokemethod, null, objdocuments, openparas );


Object opencontent = OpenDocument. GetType (). invokemember ("content", bindingflags. getproperty, null, opendocument, null );

Object openttext = opencontent. GetType (). invokemember ("text", bindingflags. getproperty, null, opencontent, null );



// Set whether the interface is displayed. The read information is not displayed.
// Object [] pars = {true };
// ObjWordApp. GetType (). InvokeMember ("Visible", BindingFlags. SetProperty, null, objWordApp, pars );


# Endregion


Return opentText. ToString ();

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.