如何在Word文件中搜索特定文本并使用Word interop在语法上进行更改?

时间:2019-01-19 13:27:48

标签: vb.net ms-word interop add-in

我需要找到从特定文本开始并以特定文本结尾的word文档范围,然后我要以特定格式编辑此范围。 例如,在我的代码中,我的文档中有一个表格,其标题以文本{TN}开头,而表格以隐藏文本{OAEndontribution}结尾。我想用表号替换文本{TN},当它找到文本{OAEndontribution}时,它应该在找到下一个{TN}时增加表号,并且应该继续。 目前,我正在为此使用Word.Range对象。 但是使用我使用的代码,第一张表的标题会在下一页显示,并且显示的表号不正确。 你能帮我这个忙吗?

     ''' <summary>
''' Format any table heading fields.
''' </summary>
''' <param name="document">PRADocument to work with</param>
Private Shared Sub SetTableFields(document As PRADocument)

    Dim tableNameSearchRange As Word.Range = document.WordDocument.Content

    With tableNameSearchRange
        .TextRetrievalMode.IncludeHiddenText = False
        .TextRetrievalMode.IncludeFieldCodes = False
        .Find.Text = "\{TN*\}?"
        .Find.MatchWildcards = True
        .Find.Forward = True
        .Find.Wrap = Word.WdFindWrap.wdFindStop
    End With

    Dim endOfContributionSearchRange As Word.Range = document.WordDocument.Content

    With endOfContributionSearchRange
        .TextRetrievalMode.IncludeHiddenText = True
        .TextRetrievalMode.IncludeFieldCodes = True
        .Find.Text = Constants.OA_END_CONTRIBUTION
        .Find.MatchWildcards = True
        .Find.Forward = True
        .Find.Wrap = Word.WdFindWrap.wdFindStop
    End With

    Dim isContinuation As Boolean = False
    Dim endOfContributionPosition As Long = 0

    Do

        ' Find the table name field
        tableNameSearchRange.Find.Execute()

        If Not tableNameSearchRange.Find.Found Then Exit Do

        ' Determine if this is continuation of a contribution or the first page of the contribution
        If endOfContributionPosition = 0 OrElse tableNameSearchRange.Start > endOfContributionPosition Then
            isContinuation = False

            endOfContributionSearchRange.Find.Execute()

            If endOfContributionSearchRange.Find.Found Then
                endOfContributionPosition = endOfContributionSearchRange.End
            End If
        Else
            isContinuation = True
        End If

        Dim pg As New OAPage
        pg.CreateFrom(tableNameSearchRange)

        Dim hasSpace As Boolean = (Right(tableNameSearchRange.Text, 1) = " ")

        If Not isContinuation Then

            Dim titleFontName As String = String.Empty
            Dim titleFontSize As Single

            ' Find the font on the paragraph marker so we can set the entire heading later
            With tableNameSearchRange.Duplicate
                .Expand(Word.WdUnits.wdParagraph)
                .Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                .MoveStart(Word.WdUnits.wdCharacter, -1)
                titleFontName = .Font.Name
                titleFontSize = .Font.Size
            End With

            Dim tableName As String = If(pg.Kind = InsertionItemType.Appendix, INSERT_TYPE_APPENDIX, If(pg.Kind = InsertionItemType.Figure, INSERT_TYPE_FIGURE, INSERT_TYPE_TABLE))
            tableNameSearchRange.Text = tableName & " . " & If(hasSpace, String.Empty, " ")

            document.InsertTableOfContentField(tableNameSearchRange, tableName)

        Else

            ' Subsequent pages put in a cross reference
            tableNameSearchRange.Expand(Word.WdUnits.wdParagraph)
            tableNameSearchRange.MoveEnd(Word.WdUnits.wdCharacter, -1)
            tableNameSearchRange.Delete()
            tableNameSearchRange.Text = String.Format(" {0}", Resources.AddInStrings.HeaderContinued)

            ' Put in a style reference
            tableNameSearchRange.Collapse(Word.WdCollapseDirection.wdCollapseStart)
            document.WordDocument.Fields.Add(tableNameSearchRange, Word.WdFieldType.wdFieldStyleRef, String.Format("""{0}""", Constants.ITEM_FOR_TOC), True)

        End If

        tableNameSearchRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        tableNameSearchRange.End = document.WordDocument.Content.End

    Loop

End Sub

0 个答案:

没有答案