【问题标题】:C#: Get index of Table after specific paragraphC#:在特定段落之后获取表的索引
【发布时间】:2017-04-06 16:18:12
【问题描述】:

假设我有以下结构:-

我想得到“1.3段落3的标题”之后的第二个表的索引,意思是下一个:-

输入是

1.3 第3段的标题

预期输出

4

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

The requested member of the collection等于四。

this 使用下一个代码的目标

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

【问题讨论】:

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


    【解决方案1】:

    您可以在 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
    

    【讨论】:

      猜你喜欢
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2022-06-22
      相关资源
      最近更新 更多