c# - 如何使用 Word.Interop 获取每个标题下的段落

标签 c# ms-word office-interop

我有一些这样的示例文本:

1 Header
   bla bla bla...
   bla bla bla...
   1.1 SubHeader
       bla bla bla...
       bla bla bla...

我阅读了 .docx 的每一页文件,我想将标题下的段落和标题存储为字典:

{"1 Header", "bla bla bla"...},
{"1.1 Subheader", "bla bla bla"...},

其中键是 string ( header 名称),值为 ParagraphsList<Paragraph>

我知道这可以用 Selection() 来完成,但我不知道如何使用它。

for (int i = 1; i<=paragraphCollection.Count;i++)
{
    if (isHeader(paragraphCollection[i].Range.Text) || isSubHeader(paragraphCollection[i].Range.Text))
    {
       //collect paragraphs until we find another header or subheader                 
    }
}

最佳答案

我通常使用 VBA 尝试算法并将其转换为 C#..

C# 很强大,但是当涉及到 MS Word 的对象引用时,VBA 更好。

阅读我的代码并将其翻译成您的 C# 代码。 我认为遍历所有段落并不可取。

(已更新)此算法适用于二维结构的字典。

如果你想要像JSON这样的结构,你需要解析文档。 在那种情况下,我认为解析 .docx xml 结构比使用 MS word 对象和方法更好。

另外,我没有考虑文档的第一段不是标题的地方。

Sub IteratingHeadingsForDoSomething()

listHeadingsString = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading) ' Collection of heading strings, No location info
headingsCount = UBound(listHeadingsString)

Selection.GoTo What:=wdGoToHeading, Which:=wdGoToFirst 'goto first heading
For i = 1 To headingsCount Step 1
    
    Debug.Print listHeadingsString(i) ' put this to the list
    Debug.Print Selection.Range.Paragraphs.OutlineLevel ' use outline level
    
    SelectNextParagraph
    If IsOutlineLevel(Selection) = True Then
        ' para under the heading is also a heading
        Debug.Print "This is a heading too" ' >>> Skip storing??
        GotoPreviousHeading   ' go back to previous heading
    Else
        Do Until IsOutlineLevel(Selection) = True
            Debug.Print Selection.Range.Text '>>> Store paragraph separately or concatenatively
            SelectNextParagraph
        Loop
        GotoPreviousHeading
    End If
    
    
    GoToNextHeading
Next i

End Sub
'//////////////////////////////////////////
Sub GoToNextHeading()

    Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext

End Sub
'//////////////////////////////////////////
Sub GotoPreviousHeading()

     Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious

End Sub
'//////////////////////////////////////////
Sub SelectNextParagraph()

    Selection.Next(Unit:=wdParagraph, Count:=1).Select
    
End Sub
'//////////////////////////////////////////
Function IsOutlineLevel(mySelection As Selection) As Boolean

    If mySelection.Range.Paragraphs.OutlineLevel = wdOutlineLevelBodyText Then
        IsOutlineLevel = False
    Else
        IsOutlineLevel = True
    End If

End Function

关于c# - 如何使用 Word.Interop 获取每个标题下的段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68008541/

相关文章:

c# - 如何使用 Word Automation 在段落中添加下标字符?

c# - Powerpoint 到文本 C# - Microsoft.Interop

c# - Json.Net 中的 "Current error context error is different to requested error"异常

c# - 使用 EPPlus 获取单元格的行号

c# - "Always running"控制台应用程序

regex - 创建正则表达式来查找带有括号首字母缩略词的句子 (VBasic Word)

jquery - HTML/JQuery : How to close browser tab automatically after word document download (ms-word:ofe|u|)?

c# - Interop Excel 未关闭进程

c# - 如何通过 Interop (C#) 向 Word 文档添加结构化内容?

c# - 两个select语句的sql查询