2016-03-03 42 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

相關問題