Advertisement
Combreal

customSearchFunction01.vba

Feb 22nd, 2021 (edited)
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Please refer to https://docs.microsoft.com/fr-fr/office/vba/api/word.wdcolorindex to get other colours values
  2. Const highLightColor1 = 4
  3. Const highLightColor2 = 11
  4. Dim savedWordAposition As Integer
  5. Dim savedWordBposition As Integer
  6.  
  7. Private Sub Userform_Initialize()
  8.     Search.TextBox1.Text = ""
  9.     Search.TextBox2.Text = ""
  10.     Search.TextBox4.Text = ""
  11.     savedWordAposition = 0
  12. End Sub
  13.  
  14. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  15.     Call CommandButton2_Click
  16.     Unload Me
  17. End Sub
  18.  
  19. Private Sub TextBox1_KeyDown(ByVal KeyAscii As MSForms.ReturnInteger, ByVal Shift As Integer)
  20.     If KeyAscii = 27 Then
  21.         Call CommandButton2_Click
  22.         Unload Me
  23.     ElseIf KeyAscii = 13 Then
  24.         Call CommandButton1_Click
  25.     End If
  26. End Sub
  27.  
  28. Private Sub TextBox2_KeyDown(ByVal KeyAscii As MSForms.ReturnInteger, ByVal Shift As Integer)
  29.     If KeyAscii = 27 Then
  30.         Call CommandButton2_Click
  31.         Unload Me
  32.     ElseIf KeyAscii = 13 Then
  33.         Call CommandButton1_Click
  34.     End If
  35. End Sub
  36.  
  37. Private Sub TextBox4_KeyDown(ByVal KeyAscii As MSForms.ReturnInteger, ByVal Shift As Integer)
  38.     If KeyAscii = 27 Then
  39.         Call CommandButton2_Click
  40.         Unload Me
  41.     ElseIf KeyAscii = 13 Then
  42.         Call CommandButton1_Click
  43.     End If
  44. End Sub
  45.  
  46. Private Sub UserForm_KeyDown(ByVal KeyAscii As MSForms.ReturnInteger, ByVal Shift As Integer)
  47.     If KeyAscii = 27 Then
  48.         Call CommandButton2_Click
  49.         Unload Me
  50.     ElseIf KeyAscii = 13 Then
  51.         Call CommandButton1_Click
  52.     End If
  53. End Sub
  54.  
  55. Private Sub CommandButton1_KeyDown(ByVal KeyAscii As MSForms.ReturnInteger, ByVal Shift As Integer)
  56.     If KeyAscii = 27 Then
  57.         Call CommandButton2_Click
  58.         Unload Me
  59.     ElseIf KeyAscii = 13 Then
  60.         Call CommandButton1_Click
  61.     End If
  62. End Sub
  63.  
  64. Private Sub CommandButton1_Click()
  65.     Dim wordA  As String
  66.     Dim wordB  As String
  67.     Dim wordAStarter  As String
  68.     Dim wordBStarter  As String
  69.     Dim lineInterval As Integer
  70.     Dim currLine As String
  71.     Dim numOfLines As Integer
  72.     Dim wordApositions() As Variant
  73.     Dim wordBpositions() As Variant
  74.     Dim wordAposSize As Integer
  75.     Dim wordBposSize As Integer
  76.     Dim flg As Boolean
  77.    
  78. function_Restart:
  79.     flg = False
  80.     ReDim wordApositions(1)
  81.     ReDim wordBpositions(1)
  82.     wordA = Search.TextBox1.Value
  83.     wordB = Search.TextBox2.Value
  84.     wordAStarter = StrConv(wordA, 3)
  85.     wordBStarter = StrConv(wordB, 3)
  86.     numOfLines = ActiveDocument.BuiltInDocumentProperties("NUMBER OF LINES")
  87.     If Len(Trim(Search.TextBox4.Value)) Then
  88.         lineInterval = CInt(Search.TextBox4.Value)
  89.     Else
  90.         lineInterval = numOfLines
  91.     End If
  92.     lineCounter = 0
  93.    
  94.     If wordA = "" And wordB = "" Then
  95.         MsgBox "Please fill at least one search word", "", vbExclamation
  96.     ElseIf Not wordA = "" And wordB = "" Then
  97.         Selection.Find.ClearFormatting
  98.         With Selection.Find
  99.             .Text = Search.TextBox1.Value
  100.             .Replacement.Text = ""
  101.             .Forward = True
  102.             .Wrap = wdFindContinue
  103.             .Format = False
  104.             .MatchCase = False
  105.             .MatchWholeWord = False
  106.             .MatchWildcards = False
  107.             .MatchSoundsLike = False
  108.             .MatchAllWordForms = False
  109.             .Execute
  110.         End With
  111.     ElseIf Not wordA = "" And Not wordB = "" Then
  112.         Selection.HomeKey Unit:=wdStory
  113.         For i = 1 To numOfLines
  114.             Selection.HomeKey Unit:=wdLine
  115.             Selection.EndKey Unit:=wdLine, Extend:=wdExtend
  116.             currLine = Selection.Range.Text
  117.             currLine = Left(currLine, (Len(currLine) - 1))
  118.             If Not IsEmpty(wordA) And Not wordA = "" Then
  119.                 If InStr(currLine, wordA) Then
  120.                     wordAposSize = UBound(wordApositions) - 1
  121.                     wordApositions(wordAposSize) = i
  122.                     wordAposSize = wordAposSize + 2
  123.                     ReDim Preserve wordApositions(wordAposSize)
  124.                 End If
  125.             End If
  126.             If Not IsEmpty(wordB) And Not wordB = "" Then
  127.                 If InStr(currLine, wordB) Then
  128.                     wordBposSize = UBound(wordBpositions) - 1
  129.                     wordBpositions(wordBposSize) = i
  130.                     wordBposSize = wordBposSize + 2
  131.                     ReDim Preserve wordBpositions(wordBposSize)
  132.                 End If
  133.             End If
  134.             If Not IsEmpty(wordAStarter) And Not wordAStarter = "" Then
  135.                 If InStr(currLine, wordAStarter) Then
  136.                     wordAposSize = UBound(wordApositions) - 1
  137.                     wordApositions(wordAposSize) = i
  138.                     wordAposSize = wordAposSize + 2
  139.                     ReDim Preserve wordApositions(wordAposSize)
  140.                 End If
  141.             End If
  142.             If Not IsEmpty(wordBStarter) And Not wordBStarter = "" Then
  143.                 If InStr(currLine, wordBStarter) Then
  144.                     wordBposSize = UBound(wordBpositions) - 1
  145.                     wordBpositions(wordBposSize) = i
  146.                     wordBposSize = wordBposSize + 2
  147.                     ReDim Preserve wordBpositions(wordBposSize)
  148.                 End If
  149.             End If
  150.             Selection.MoveDown Unit:=wdLine, Count:=1
  151.         Next i
  152.        
  153.         If Not IsEmpty(wordA) And Not wordA = "" And Not IsEmpty(wordB) And Not wordB = "" Then
  154.             If savedWordAposition = UBound(wordApositions) Or wordApositions(savedWordAposition) = "" Then
  155.                 savedWordAposition = 0
  156.             End If
  157.             For j = savedWordAposition To UBound(wordApositions)
  158.                 For k = LBound(wordBpositions) To UBound(wordBpositions)
  159.                     If Not wordBpositions(k) = "" And Not wordApositions(j) = "" And wordBpositions(k) >= wordApositions(j) - (lineInterval / 2) And wordBpositions(k) <= wordApositions(j) + (lineInterval / 2) Then
  160.                         If (wordApositions(j) >= wordApositions(j - 1) - 3) And (wordApositions(j) <= wordApositions(j - 1) + 3) Then
  161.                             savedWordAposition = j + 1
  162.                             GoTo function_Restart
  163.                         End If
  164.                         Call HighLightText(wordA, wordB)
  165.                         Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=wordApositions(j)
  166.                         savedWordAposition = j + 1
  167.                         flg = True
  168.                         Exit For
  169.                     End If
  170.                 Next k
  171.                 If flg = True Then
  172.                     Exit For
  173.                 End If
  174.             Next j
  175.         End If
  176.     End If
  177. End Sub
  178.  
  179. Private Sub CommandButton2_Click()
  180.     savedWordAposition = 0
  181.     Selection.HomeKey Unit:=wdStory
  182.     With Selection.Find
  183.         .ClearFormatting
  184.         .Replacement.ClearFormatting
  185.         .Text = ""
  186.         .Replacement.Text = ""
  187.         .Forward = True
  188.         .Wrap = wdFindStop
  189.         .Format = False
  190.         .MatchCase = False
  191.         .MatchWholeWord = False
  192.         .MatchWildcards = False
  193.         .MatchSoundsLike = False
  194.         .MatchAllWordForms = False
  195.     End With
  196.     For Each StoryRange In ActiveDocument.StoryRanges
  197.         StoryRange.HighlightColorIndex = wdNoHighlight
  198.     Next StoryRange
  199.     Application.Selection.EndOf
  200. End Sub
  201.  
  202. Sub HighLightText(wordA As String, wordB As String)
  203. Dim Word As Range
  204.     Dim WordCollection(1) As String
  205.     Dim Colors(1) As WdColorIndex
  206.     Dim CurrentColor As WdColorIndex
  207.     Dim i As Long
  208.  
  209.     WordCollection(0) = wordA
  210.     WordCollection(1) = wordB
  211.     Colors(0) = highLightColor1
  212.     Colors(1) = highLightColor2
  213.     CurrentColor = Options.DefaultHighlightColorIndex
  214.     Application.ScreenUpdating = False
  215.     With ActiveDocument.Content.Find
  216.         .ClearFormatting
  217.         .Replacement.ClearFormatting
  218.         .Replacement.Text = ""
  219.         .Forward = True
  220.         .Wrap = wdFindContinue
  221.         .Format = True
  222.         .MatchCase = False
  223.         .MatchWholeWord = True
  224.         .MatchWildcards = False
  225.         .MatchSoundsLike = False
  226.         .MatchAllWordForms = False
  227.         .Replacement.Highlight = True
  228.         For i = 0 To 1
  229.             Options.DefaultHighlightColorIndex = Colors(i)
  230.             .Execute FindText:=WordCollection(i), Replace:=wdReplaceAll
  231.         Next i
  232.     End With
  233.     Application.ScreenUpdating = True
  234.     Options.DefaultHighlightColorIndex = CurrentColor
  235. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement