asp.net to manipulate word (simple)

Source: Internet
Author: User
Asp.net|word

First you add a reference: COM----> Microsoft Word 9.0 Object Library
To use this namespace: System.Reflection

void Opword ()
{

Word. Application oword;
Word. _document odoc;
Object Docbuiltinprops;
object omissing = Missing.Value;
Object Source = Server.MapPath ("Liu.doc");
Object Unknown =type.missing;
oword = new Word. Application ();

Try
{
odoc = OWord.Documents.Open (ref source,ref Unknown,
Ref Unknown,ref Unknown,ref Unknown,
Ref Unknown,ref Unknown,ref Unknown,
Ref Unknown,ref Unknown,ref Unknown,
Ref Unknown);

The following code can get word text
Response.Write (ODoc.Words.Count.ToString ());
Response.Write ("

");
Response.Write (ODoc.Words.Application.Documents.ToString ());
Object a=0;
Object B= (object) ODoc.Words.Count;
Word. Range Oooo=odoc.range (ref a,ref B);
Response.Write (oooo. Text);
Response.Write (" ");
//----------------------------


Docbuiltinprops = odoc.builtindocumentproperties;

Type typedocbuiltinprops = Docbuiltinprops.gettype ();


Get author
String index = "Author";
String Propsvalue;

Object DocAuthor = Typedocbuiltinprops.invokemember ("Item",
Bindingflags.default |
BindingFlags.GetProperty,
Null,docbuiltinprops,
New object[] {index});

Type typeDocAuthorProp = Docauthor.gettype ();

Propsvalue = typeDocAuthorProp.InvokeMember ("Value"),
Bindingflags.default |
BindingFlags.GetProperty,
Null,docauthor,
New object[] {}). ToString ();

Response.Write (Propsvalue);

Get a topic
index = "Subject";
Propsvalue = "The Subject";

Object Docsubjectprop =

Typedocbuiltinprops.invokemember ("Item"),
Bindingflags.default |
BindingFlags.GetProperty,
Null,docbuiltinprops,
New object[] {index});

Type Typedocsubjectprop = Docsubjectprop.gettype ();

Propsvalue = Typedocsubjectprop.invokemember ("Value"),
Bindingflags.default | BindingFlags.GetProperty,
Null,docsubjectprop,
New object[] {}). ToString ();

Response.Write (Propsvalue);
}
Finally
{
Close Word process
Object save = false;

Oword.quit (ref save,ref unknown,ref Unknown);

System.Runtime.InteropServices.Marshal.ReleaseComObject (oword);
}
}



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.