Advertisement
Combreal

evolvedSearchWordV1.vba

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