C#:获取特定段落后的表索引

时间:2016-11-22 20:40:05

标签: c# .net office-interop office-2007

假设我有以下结构: -

enter image description here

我希望得到第二个表的索引" 1.3第3段和第34段的标题,表示下一个: -

输入

  

1.3第3段的标题

预期输出

  

4

这是文件中的第四个表格,或者你可以说

The requested member of the collection等于四。

使用下一个代码的目标

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

1 个答案:

答案 0 :(得分: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