2016-03-03 68 views
0

如何通过变量的文本搜索某些字符串?搜索某些字符串的变量文本

在Excel VBA,如果我想剥离记数的文本myTextVariable = "ii. The man walks away",我可以做

myTextVariable = Left(myTextVariable,Search(".",myTextVariable))这将有效地让我"The man walks away"

如何在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 

但是现在有一个问题:有时文本将"The U.S. - a country blah",这将切出的“U”。我如何才能搜索,如果它是i, ii, ..., x?我可以用InStr使用RegEx吗?我当然可以用i, ii, iii, etc做一个数组并遍历数组,但认为这可能不是最有效的方法。

感谢ScottCraner,我可以用下面得到它:

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 
+1

岂不是正确的,而不是留?我更喜欢中间到右边,'myTextVariable = Mid(myTextVariable,Instr(myTextVariable,“。”)+ 1)' –

+0

@ScottCraner - 我也有这个想法。我现在正在测试并将回报。 – BruceWayne

+0

@ScottCraner - 效果很棒!现在唯一的事情是我必须在开始时辨别“i.'和”U.S.“。我知道,因为'我'是开始,我可以缩小到前10个字符(因此我的'如果periodLoc <10'线)我想我可以使用正则表达式,但不知道你是否可以在'InStr'中,但是会尝试。 – BruceWayne

回答

2

随着道歉@ScottCraner,我早就有function to convert roman numerals to arabic工作,认为上面的代码可能会利用该功能。 (感谢mdmackkillop很久以前使用该功能。)请将该功能复制到您的模块中。

下面的代码应该对任何罗马格式的数字工作(只要罗马数字少于十个字符 - 一个安全的赌注)

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 
+0

哦,非常时髦!非常感谢! – BruceWayne

+1

我的自我并不那么大,我无法欣赏更好的方法。 –