首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在word文档中突出显示单词的程序

在word文档中突出显示单词的程序
EN

Stack Overflow用户
提问于 2013-10-17 10:25:29
回答 1查看 791关注 0票数 0

我想要一个VB程序,它可以让我将单词放入VB的文本框中(最好是在关闭程序时保存这些单词,以便下次使用)让这些单词更有可能被s.Split拆分,以允许多个单词,例如“饥荒”,“查看”,“文章”

这是第一部分,第二部分是我想让程序读取任何打开的word文档中的文本,文本框中的那些单词,也在word文档中,将被突出显示。如果它不能阅读任何打开的word文档,那么有没有可能让它附加一个word文档以供其阅读?

我想要一个程序,你可以在上面写多个单词,然后有这些单词在任何word文档中突出显示。

EN

回答 1

Stack Overflow用户

发布于 2013-10-17 16:47:38

我以前写过这段代码,它将教你如何使用正则表达式查找和突出显示单词/短语,你可以很容易地将其适应于WORD文档阅读,然后问题的最重要部分将通过以下方式解决:

代码语言:javascript
复制
#Region " [RichTextBox] FindNext RegEx "

' [ FindNext RegEx ]
'
' //By Elektro H@cker
'
' Examples :
'
' RichTextBox1.Text = "Hello World!, Hello World!, Hello World!"
'
' FindNext(RichTextBox1, "hello", FindDirection.Down, System.Text.RegularExpressions.RegexOptions.IgnoreCase, Color.LightBlue, Color.Black)
' FindNext(RichTextBox1, "hello", FindDirection.Up, System.Text.RegularExpressions.RegexOptions.IgnoreCase, Color.Red, Color.Black)
'
' Private Sub RichTextBox_Enter(sender As Object, e As EventArgs) ' Handles RichTextBox1.Enter
'    ' Restore Selection Colors before search next match.
'    sender.SelectionBackColor = DefaultBackColor
'    sender.SelectionColor = DefaultForeColor
' End Sub

Public Enum FindDirection As Short
    Up = 0
    Down = 1
End Enum

' FindNext
Private Sub FindNext(ByVal [Control] As RichTextBox, _
                           ByVal SearchText As String, _
                           ByVal Direction As FindDirection, _
                           Optional ByVal IgnoreCase As System.Text.RegularExpressions.RegexOptions = System.Text.RegularExpressions.RegexOptions.None, _
                           Optional ByVal Highlight_BackColor As Color = Nothing, _
                           Optional ByVal Highlight_ForeColor As Color = Nothing)

    If [Control].TextLength = 0 Then Exit Sub

    ' Start searching at 'SelectionStart'.
    Dim Search_StartIndex As Integer = [Control].SelectionStart

    ' Stores the MatchIndex count
    Dim matchIndex As Integer = 0

    ' Flag to check if it's first find call
    Static First_Find As Boolean = True

    ' Checks to don't ommit the selection of first match if match index is exactly at 0 start point.
    If First_Find _
        AndAlso Search_StartIndex = 0 _
        AndAlso Direction = FindDirection.Down Then
        Search_StartIndex = -1
        First_Find = False
    ElseIf Not First_Find _
        AndAlso Search_StartIndex = 0 _
        AndAlso Direction = FindDirection.Down Then
        First_Find = False
        Search_StartIndex = 0
    End If

    ' Store the matches
    Dim matches As System.Text.RegularExpressions.MatchCollection = _
        System.Text.RegularExpressions.Regex.Matches([Control].Text, _
                                                     SearchText, _
                                                     IgnoreCase Or If(Direction = FindDirection.Up, _
                                                                      System.Text.RegularExpressions.RegexOptions.RightToLeft, _
                                                                      System.Text.RegularExpressions.RegexOptions.None))

    If matches.Count = 0 Then First_Find = True : Exit Sub

    ' Restore Highlight colors of previous selection
    [Control].SelectionBackColor = [Control].BackColor
    [Control].SelectionColor = [Control].ForeColor

    ' Set next selection Highlight colors
    If Highlight_BackColor = Nothing Then Highlight_BackColor = [Control].BackColor
    If Highlight_ForeColor = Nothing Then Highlight_ForeColor = [Control].ForeColor

    ' Set the match selection
    For Each match As System.Text.RegularExpressions.Match In matches

        matchIndex += 1

        Select Case Direction

            Case FindDirection.Down
                If match.Index > Search_StartIndex Then ' Select next match
                    [Control].Select(match.Index, match.Length)
                    Exit For
                ElseIf match.Index <= Search_StartIndex _
                AndAlso matchIndex = matches.Count Then ' Select first match
                    [Control].Select(matches.Item(0).Index, matches.Item(0).Length)
                    Exit For
                End If

            Case FindDirection.Up
                If match.Index < Search_StartIndex Then ' Select previous match
                    [Control].Select(match.Index, match.Length)
                    Exit For
                ElseIf match.Index >= Search_StartIndex _
                AndAlso matchIndex = matches.Count Then ' Select last match
                    [Control].Select(matches.Item(0).Index, matches.Item(0).Length)
                    Exit For
                End If

        End Select

    Next match

    ' Set the current selection BackColor
    [Control].SelectionBackColor = Highlight_BackColor
    ' Set the current selection ForeColor
    [Control].SelectionColor = Highlight_ForeColor
    ' Scroll to Caret/Cursor selection position
    [Control].ScrollToCaret()

End Sub

#End Region

此外,您还可以在此处看到视频演示:http://www.youtube.com/watch?v=mWRMdlC5DH8

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19417223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档