Vba 在变量文本中搜索某个字符串

Vba 在变量文本中搜索某个字符串,vba,ms-word,Vba,Ms Word,如何在变量的文本中搜索某个字符串 在Excel VBA中,如果我想去掉文本myTextVariable=“ii.男人走开”的计算,我可以这样做 myTextVariable=Left(myTextVariable,Search(“.”,myTextVariable))这会给我留下“男人走开了” 我怎样才能在Word中做到这一点?我尝试了类似于myTextVariable=left(myTextVariable,Find.Text=“.”)的方法,但没有成功。还尝试了类似于myTextVariab

如何在变量的文本中搜索某个字符串

在Excel VBA中,如果我想去掉文本
myTextVariable=“ii.男人走开”
的计算,我可以这样做

myTextVariable=Left(myTextVariable,Search(“.”,myTextVariable))
这会给我留下
“男人走开了”

我怎样才能在Word中做到这一点?我尝试了类似于
myTextVariable=left(myTextVariable,Find.Text=“.”)的方法,但没有成功。还尝试了类似于
myTextVariable.Content.Find.Execute(findText:=”)
的方法,但没有效果

总体思路是我有一个包含许多条目的索引:

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
我正在循环浏览它们,想删除文本之前的任何
I,ii,iii,iv,v,…,x
,但不知道如何做

谢谢你的建议

编辑:感谢@ScottCraner,我现在正在使用:

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

在向@ScottCraner道歉之后,我很久以前就使用了一个,并且认为上面的代码可能会利用这个函数。(感谢mdmackkillop很久以前使用了该功能。)请将该功能复制到您的模块中

以下代码适用于任何罗马格式的数字(只要罗马数字少于10个字符,这是一个安全的赌注)

选项显式
函数StripNumeration(ByVal myRange作为范围)作为字符串
“---假定myRange参数值可能包含前导字符
“数字。返回不带
"头号",
暗周期与长周期相同
作为字符串的数字
将文本设置为字符串
returnText=myRange.Text
periodLoc=InStr(1,returnText,“.”,vbTextCompare)
如果periodLoc<10且periodLoc>0,则
计数=左(返回文本,周期LOC-1)
如果阿拉伯语(计数)“失败”,则
returnText=右(returnText,Len(returnText)-周期定位)
如果结束
其他的
如果结束
条带编号=修剪(返回文本)
端函数

我向@ScottCraner道歉,我很久以前就使用过一个,我认为上面的代码可能会利用这个功能。(感谢mdmackkillop很久以前使用了该功能。)请将该功能复制到您的模块中

以下代码适用于任何罗马格式的数字(只要罗马数字少于10个字符,这是一个安全的赌注)

选项显式
函数StripNumeration(ByVal myRange作为范围)作为字符串
“---假定myRange参数值可能包含前导字符
“数字。返回不带
"头号",
暗周期与长周期相同
作为字符串的数字
将文本设置为字符串
returnText=myRange.Text
periodLoc=InStr(1,returnText,“.”,vbTextCompare)
如果periodLoc<10且periodLoc>0,则
计数=左(返回文本,周期LOC-1)
如果阿拉伯语(计数)“失败”,则
returnText=右(returnText,Len(returnText)-周期定位)
如果结束
其他的
如果结束
条带编号=修剪(返回文本)
端函数

它是否应该是右侧而不是左侧?我更喜欢从中间到右边,
myTextVariable=mid(myTextVariable,Instr(myTextVariable,”)+1)
您只需要使用
I.
v.
x.
循环即可。其他7种组合只能在两个字符串上使用。@ScottCraner-聪明!!这正是我所做的,它似乎正在发挥作用。我已经把我的最后一段代码放在了我的OP中。如果你想把你的评论变成一个答案,我很乐意为你做标记。正确地使用“I.”可以让你将“hi.blah,blah”转换为“blah,blah”,例如,“替换”、“选择…”和“拆分”(仅适用于VBA 5或6)可能会有所帮助。我不是你在正则表达式上的那个人:-)它应该是对的,而不是左的吗?我更喜欢从中间到右边,
myTextVariable=mid(myTextVariable,Instr(myTextVariable,”)+1)
您只需要使用
I.
v.
x.
循环即可。其他7种组合只能在两个字符串上使用。@ScottCraner-聪明!!这正是我所做的,它似乎正在发挥作用。我已经把我的最后一段代码放在了我的OP中。如果你想把你的评论变成一个答案,我很乐意为你做标记。正确地使用“I.”可以让你将“hi.blah,blah”转换为“blah,blah”,例如,“替换”、“选择…”和“拆分”(仅适用于VBA 5或6)可能会有所帮助。我不是你在regex上的那个人:-)我的自尊心没有那么大,以至于我不能欣赏更好的方法。我的自尊心没有那么大,以至于我不能欣赏更好的方法。
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
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