【发布时间】:2014-01-13 10:10:55
【问题描述】:
我尝试创建一个函数来搜索文档中的字符串并检查字符串中红色的第一个字符是什么。
例如,我知道我的文档包含字符串“面包水果汁桃酒”。想象一下粗体文本是红色的。我希望函数返回 int 19(第一个红色字符 - p)。
Function check(stringToCheck As String) As Integer
Dim oRng As Word.Range
Set oRng = ActiveDocument.Content
With oRng.Find
' to ensure that unwanted formats aren't included as criteria
.ClearFormatting
'You don't care what the text is
.Text = stringToCheck
'Loop for each match and set a color
While .Execute
MsgBox (oRng.Text)
For i = 1 To 40
'take the Nth char of the string an check if it's red
'the following msgBox is working
MsgBox (Mid(oRng, i, 1))
If Mid(Orng, i, 1).Font.Color = wdColorRed Then
'the following msgBox is not working which means the error is in the last line.
MsgBox ("made it")
check = i
Exit Function
End If
Next i
Wend
End With
End Function
每次我尝试调用该函数时都会出现错误“运行时错误 424 - 需要对象”。
我添加了一些 msgboxes 以查看函数何时中断并在该位置添加了注释。
有什么问题?我该如何解决?
【问题讨论】:
标签: string vba search colors ms-word