读取Word文档的标题
一:描述,将读取的文档标题添加到下拉框中
二:代码
#region 方法:得到Word文档标题的内容 public static List<string> GetTitles(int j,int m)//标题深度,标题字符个数选取标准 { List<string> title = new List<string>(); for (int i = 0; i <= j; i++)//标题深度 { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = null; object fileName = "D://1.doc"; object unknow = Type.Missing; app.Visible = false; doc = app.Documents.Open(ref fileName, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow); for (int k = 1; k <= doc.Paragraphs.Count; k++) { if(doc.Paragraphs[k].Range.Text.Trim().Length>m) title.Add(doc.Paragraphs[k].Range.Text.Trim()); } object unknowFirst = Type.Missing; object saveChangesFirst = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges; app.ActiveDocument.Close(ref saveChangesFirst, ref unknowFirst, ref unknowFirst); object unknowSec = Type.Missing; object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges; app.Quit(ref saveChanges, ref unknowSec, ref unknowSec); } return title; } #endregion