How to search for a particular text in the word file and Pro-grammatically change it using Word interop?

五迷三道 提交于 2019-12-11 17:55:25

问题


I need to find range of the word document starting from particular text and ends with particular text then I want to edit this range in particular format. e.g In my code I have a Table in the document whose header starts with text {TN} and Table ends with hidden text {OAEndontribution}. I want to replace text {TN} with table number and when it finds text {OAEndontribution} then it should increment table number when it finds next {TN} and this should continue. Currently I am using Word.Range object for this. But with the code I am using the Title of first Table gets carried over the next page and also displayed table number is incorrect. Will you please help me with this?

     ''' <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

来源:https://stackoverflow.com/questions/54267591/how-to-search-for-a-particular-text-in-the-word-file-and-pro-grammatically-chang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!