C # quick access to the Word document structure

Source: Internet
Author: User

After searching the Internet for half a day, there is no straightforward method, either it is slow or it is terrible slow. by thinking about it, you can finally get the structure of the Word document quickly. Take a shower, (* ^__^ *), and you will see it .......

[Csharp]
Private void barButtonItem24_ItemClick (object sender, DevExpress. XtraBars. ItemClickEventArgs e)
{
Microsoft. Office. Interop. Word. Document doc = (Microsoft. Office. Interop. Word. Document) (this. axFramerControl1.ActiveDocument );
 
Object x = doc. GetCrossReferenceItems (Microsoft. Office. Interop. Word. WdReferenceType. wdRefTypeHeading );
Array strs = (Array) x;
Int pid = 0;
Int id = 0;
System. Data. DataTable dt = new PrintHelper (). DTStruct ();
// Doc. Application. ScreenUpdating = false;
Int UpLevel = 0;
DataRow drUP = null;
For (int I = 0; I <strs. Length; I ++)
{
Object count = I + 1;
String str = strs. GetValue (I + 1). ToString ();
String strtrm = str. TrimStart ();
DataRow dr = dt. NewRow ();
Dr ["SortFlag"] = I + 1;
Dr ["ParagraphIndex"] = I + 1;
Dr ["Name"] = strtrm;
Int level = str. Length-strtrm. Length;
Dr ["Level"] = level;
If (level = 0)
{
Dr ["ParentID"] = 0;
UpLevel = 0;
Object Heading = (int) Microsoft. Office. Interop. Word. WdGoToItem. wdGoToHeading;
Doc. Application. Selection. GoTo (ref Heading, ref missing, ref count, ref missing );
Dr ["SectionNumber"] = doc. Application. Selection. get_Information (Microsoft. Office. Interop. Word. WdInformation. wdActiveEndSectionNumber );

}
Else
{
If (level-UpLevel = 0)
{
Dr ["ParentID"] = drUP ["ParentID"];
Dr ["SectionNumber"] = drUP ["SectionNumber"];

}
Else
If (level-UpLevel> 0)
{
Dr ["ParentID"] = drUP ["ID"];
Dr ["SectionNumber"] = drUP ["SectionNumber"];
}
Else
{
Dr ["ParentID"] = ParetnID (I-1, dt, level) ["ParentID"];
Dr ["SectionNumber"] = ParetnID (I-1, dt, level) ["SectionNumber"];
}


}
Dt. Rows. Add (dr );
UpLevel = level;
DrUP = dr;
}
// Doc. Application. ScreenUpdating = true;
 
}
DataRow ParetnID (int rowindex, System. Data. DataTable dt, int level)
{
// Object parentID = DBNull. Value;
DataRow dr = dt. NewRow ();
For (int I = rowindex; I> = 0; I --)
{
If (dt. Rows [I] ["Level"]. ToString () = level. ToString ())
{
Dr = dt. Rows [I];
Break;
} Www.2cto.com
}
Return dr;
}

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.