2016-11-22 103 views
0

假设我有以下结构: -C#:特定段落后获取表的索引

enter image description here

我想获得第二个表的“1.3款3的标题”后的指数,意味着未来: -

输入是

1.3款3的标题

预期的输出

这是在文档中第四表,或者可以说

The requested member of the collection equlas四强。

的这个使用下面的代码

Microsoft.Office.Interop.Word.Application app = new  Microsoft.Office.Interop.Word.Application(); 
Documents docs = app.Documents; 
Document doc = docs.Open(sDocPath, ReadOnly: true); 
Table t = doc.Tables[4] // 4 that what I need 

回答

0

你可以尝试用VB.NET下面的代码,或者干脆将其转换为C#的目标。

<TestMethod()> Public Sub getDocText() 
    Dim filepath As String = "C:\Test Table.docx" 
    If File.Exists(filepath) AndAlso (Path.GetExtension(filepath).ToUpper.Equals(".DOCX") Or Path.GetExtension(filepath).ToUpper.Equals(".DOC")) Then 
    Dim app As Word.Application = New Word.Application 
    Dim doc As Word.Document = app.Documents.Open(filepath) 
    Dim topic1Range As Word.Range = doc.Content 
    Dim topic2Range As Word.Range = doc.Content 
    Dim Find As Word.Find = topic1Range.Find() 
    Find.Execute("1.2 The headline of paragraph3") 

    Dim Find2 As Word.Find = topic2Range.Find() 
    Find2.Execute("1.3 The headline of paragraph3") 

    Dim contentRange As Word.Range = doc.Range(topic1Range.End, topic2Range.Start) 
    MsgBox(contentRange.Tables.Count) 
    app.Quit() 
    End If 
End Sub