BruceWayne
BruceWayne

Reputation: 23283

Search variable text for some string

How can I search through a variable's text for some string?

In Excel VBA, if I wanted to strip the text myTextVariable = "ii. The man walks away" of the numeration, I could do

myTextVariable = Left(myTextVariable,Search(".",myTextVariable)) which would effectively leave me with "The man walks away".

How can I do that in Word? I tried something like myTextVariable = left(myTextVariable, Find.Text = ".") but that doesn't work. Also tried like myTextVariable.Content.Find.Execute(findText:=".") to no avail.

The overall idea is I have an index with many entries:

Automobile - type of car
Art Deco - an art type from back in the day
USA - a country
ii.Australia -a continent
iv. Greenland - another continent
Greenland - an icy continent

And I'm looping through them, and want to remove any i, ii, iii, iv, v, ..., x before the text and can't figure how to do so.

Thanks for any ideas!

Edit: Thanks to @ScottCraner, I'm now using:

myTextVariable = myRange.Text
periodLoc = InStr(myTextVariable, ".")
If periodLoc < 10 And periodLoc > 0 Then
    finalText = Trim(Mid(myTextVariable, InStr(myTextVariable, ".") + 1)) ' Trim(Right(myTextVariable, Len(myTextVariable) - periodLoc))
Else
finalText = myRange.Text
End If

But now have an issue: sometimes the text will be "The U.S. - a country blah", and this will cut out "The U.". How can I search for ONLY if it's i, ii, ..., x? Can I use RegEx with InStr? I could of course do an array with i, ii, iii, etc and loop through the array, but think that might not be the most efficient way.

Thanks to ScottCraner, I was able to get it with the below:

Private Sub add_Selection_to_Index(ByVal myRange As Word.Range)
Dim textToPeriod$, finalText$
Dim periodLoc&

Debug.Print "Selection: " & myRange.Text

textToPeriod = myRange.Text
periodLoc = InStr(textToPeriod, "i.")
If periodLoc < 10 And periodLoc > 0 Then
    finalText = Trim(Right(textToPeriod, Len(textToPeriod) - periodLoc - 1))
Else
    periodLoc = InStr(textToPeriod, "v.")
    If periodLoc < 10 And periodLoc > 0 Then
        finalText = Trim(Right(textToPeriod, Len(textToPeriod) - periodLoc - 1))    ' Trim(Mid(textToPeriod, InStr(textToPeriod, "v.")))
    Else
        periodLoc = InStr(textToPeriod, "x.")
        If periodLoc < 10 And periodLoc > 0 Then
            finalText = Trim(Right(textToPeriod, Len(textToPeriod) - periodLoc - 1))    'Trim(Mid(textToPeriod, InStr(textToPeriod, "x.")))
        Else
            finalText = myRange.Text
        End If
    End If

End If

ActiveDocument.Indexes.MarkEntry Range:=myRange, entry:=finalText, entryautotext:=finalText, crossreferenceautotext:="", _
                                 bookmarkname:="", Bold:=False, Italic:=False, reading:=""
Debug.Print "Index: " & finalText
End Sub

Upvotes: 0

Views: 771

Answers (1)

PeterT
PeterT

Reputation: 8557

With apologies to @ScottCraner, I had long ago worked with a function to convert roman numerals to arabic and thought the above code might leverage that function. (Thanks to mdmackkillop for the use of the function there long ago.) Please copy that function into your module.

The following code should work for any roman-formatted number (as long as the roman number has fewer than ten characters - a safe bet)

Option Explicit

Function StripNumeration(ByVal myRange As Range) As String
    '--- assumes the myRange parameter value may contain leading roman
    '    numerals. Returns a string of the input text without the
    '    leading number
    Dim periodLoc As Long
    Dim numeration As String
    Dim returnText As String
    returnText = myRange.Text
    periodLoc = InStr(1, returnText, ".", vbTextCompare)
    If periodLoc < 10 And periodLoc > 0 Then
        numeration = Left(returnText, periodLoc - 1)
        If Arabic(numeration) <> "Fail" Then
            returnText = Right(returnText, Len(returnText) - periodLoc)
        End If
    Else
    End If
    StripNumeration = Trim(returnText)
End Function

Upvotes: 2

Related Questions