Link to home
Start Free TrialLog in
Avatar of Fi69
Fi69

asked on

Help working with Array

Hi experts

I'm not very familiar with working with arrays, so I'm hoping you can help.

I have some code that is building an array of text inside my document that is contained in brackets and is the length of one word. I want to add another condition that checks to see if I've already got that word in my array, and if so, skip.

Below is my code - I've placed commented text where I'd need this to go :  '''''if strWord already exists in this array then skip over

How would I do that?

Function ReturnBracketArray() As String()
  
    Dim strWord As String
    Dim aBracketText() As String
    Dim iCount As Long
    Dim lngIndex As Long
    Dim vSplit As Variant
    Dim bTest As Boolean

    On Error GoTo ReturnArray_Err
    
    lngIndex = 0
    Set objdoc = ActiveDocument
    Set objSelection = objdoc.Range
    
    
    objSelection.Find.Forward = True
    objSelection.Find.MatchWildcards = True
    objSelection.Find.Text = "\(*\)"
    
    Do While True
        objSelection.Find.Execute
        If objSelection.Find.Found Then
            strWord = objSelection.Text
            strWord = Replace(Replace(strWord, "(", ""), ")", "")
            strWord = Replace(Replace(strWord, Chr(145), ""), Chr(146), "")
            ReDim aBracketText(0 To 2)
            bTest = CountWords(strWord, iCount, vSplit)
            If iCount < 2 Then
                MsgBox strWord
                '''''if strWord already exists in this array then skip over
                aBracketText(lngIndex) = strWord
                lngIndex = lngIndex + 1
            End If
        Else
            Exit Do
        End If
    Loop
    
   ' Resize to current value of lngIndex - 1.
   ReDim Preserve aBracketText(0 To lngIndex - 1)
   ReturnBracketArray = aBracketText

ReturnArray_End:
   Exit Function

ReturnArray_Err:
   ' If upper bound is exceeded, enlarge array.
   If Err.Number = 9 Then ' Subscript out of range
      ' Double the size of the array.
      ReDim Preserve aBracketText(lngIndex * 2)
      Resume
   Else
      MsgBox "An unexpected error has occurred!", vbExclamation
      Resume ReturnArray_End
   End If


End Function

Function CountWords(sInput As String, iWords As Long, vWords As Variant) As Boolean
vWords = Split(sInput, " ")
iWords = UBound(vWords) + 1 - LBound(vWords)
CountWords = True
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fi69
Fi69

ASKER

Thank you for that. To get that to work I've added Error Handling as if the word exists in the dictionary it errors.