2016-03-03 40 views
0

我有一个范围(rng),其中包含单词“means”。我试图确定“means”之前的两个单词是否加下划线,但不能完全弄清楚。从一个范围内,返回一个特定单词的索引

这里是我的rng.Text是什么(请注意括号表示带下划线的文本)

"[Automobile] - means a car that isn't a bus but can be an SUV"

有时,它是"The way you have to go about it is with the various means of thinking"

第一个是一个定义,因为它有一个带有下划线的单词的“手段”。第二个例子不是一个定义。

我试图让我的宏看起来2个字之前“意味着”,但不能完全弄清楚如何。

我能够通过这个推测是多少个字符:

Dim meansLoc& 
meansLoc = instr(rng.Text, "means") 

然后,我可以测试If rng.Characters(meansLoc-9).Font.Underline = wdUnderlineSingle,但我碰到的问题,如果我定义的话只说3个字符(“爸爸 - 手段一个父亲“,会错误我们的意思,因为这意味着'索引是7,并且7-9 = -2)。这就是为什么我想用文字。 (我可以在“手段”之前使用一两个单词)。

如何返回我的rng中“means”的字符索引。如何从我的rng获得“单词索引”(即2)?

+1

记住MoveEnd?那么,还有一个使用相同参数的Move方法。看看帮助中的内容,看看WdUnits Enum并试一试:-)向后移动Range(负数值),得到它的Word(Range.Words(1)),然后测试Font.Underline .. –

回答

1

字符和单词都是范围,所以一种方法是比较字符范围的开始和每个单词在单词中的比例。你可以先从

' assumes you have already declared and populated rng 

Dim bDefinition As Boolean 
Dim i as Integer 
Dim meansLoc as Integer 
Dim meansStart as Integer 
meansLoc = instr(rng.Text,"means") 
meansStart = rng.Characters(meansLoc).Start 
bDefinition = False 
For i = 1 To rng.Words.Count 
    If rng.Words(i).Start = meansStart Then ' i is your Word index (i.e. 3, not 2!) 
    If i > 2 Then 
     If rng.Words(i - 2).Font.Underline = wdUnderlineSingle Then 
     Debug.Print "Looks like a definition" 
     bDefinition = True 
     Exit For 
     End If 
    End If 
    End If 
Next 
If Not bDefinition Then 
    Debug.Print "Couldn't see a definition" 
End If 

记住的是Word认为是一个“字”可能是从一个什么样的“字”正常的理解,不同的只是熊。

+0

这真的很时髦,谢谢! – BruceWayne

相关问题