更新:按照下面Cindy的建议,我使用了InRange函数。我的函数通过“查找”操作可以很好地进行迭代。但是,当选择超出指定范围时,该函数将无法返回FALSE。请参阅下面的“此处失败”。谢谢。
使用Visual Basic,我需要验证Word文档中的选择位置是否在命名范围内。很多年前,我使用以下代码来做到这一点:
ActiveDocument.Bookmarks("typdef").Select
While ((WordBasic.CmpBookmarks("\Sel", "typedef") = 8 _
Or WordBasic.CmpBookmarks("\Sel", "typedef") = 6 _
Or WordBasic.CmpBookmarks("\Sel", "typedef") = 10) _
And leaveloop <> 1
...
If WordBasic.CmpBookmarks("\Sel", "\EndOfDoc") = 0 Then
leaveloop = 1
End If
Wend
这是我编写的更新函数:
Function FormatSpecHeadReturn(strStyle)
Dim rngBookmark As Word.Range
Dim rngSelection As Word.Range
Set rngBookmark = ActiveDocument.Bookmarks("SpecBodyPairRange").Range
Set rngSelection = Selection.Range
var = rngSelection.InRange(rngBookmark)
Debug.Print var
Do While rngSelection.InRange(rngBookmark) = True
Selection.Find.Style = ActiveDocument.Styles(strStyle)
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey
' FAILING HERE: Returns TRUE when selection point
' is outside SpecBodyPairRange
var = rngSelection.InRange(rngBookmark)
Debug.Print var
Selection.HomeKey Unit:=wdLine, Extend:=wdMove
Selection.InsertBefore Chr(182)
Selection.EndKey
Selection.InsertAfter vbTab
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove
If rngSelection.InRange(rngBookmark) <> True Then Exit Do
Loop
End Function
我在当前项目中使用了CmpBookmarks,但是它没有可靠地返回当前位置的值。当选择点在指定范围内时,它将在两个循环中返回8,然后返回6。当选择点在指定范围之外时,CmpBookmarks将返回6。
显然,不建议使用CmpBookmarks。我找不到CmpBookmarks产生的返回值,也找不到现代的等效函数。
我承认我不了解命名的“ SpecBodyPairRange”范围与分配给r的范围之间的区别,在这里:
将范围设为昏暗
我可以看到在这种情况下,“ r”似乎可以容纳整个文档。我在Microsoft.Office.Interop.Word上研究了范围接口和选择接口,但我尚未完全了解。我不是程序员,而是一位半自动化的作家,他们自学某些编码,具有自动执行文档转换的任务。
必须有一种更好的方法来比较选择点,以验证选择点是否在指定范围内,但我找不到它。衷心感谢您能给我的任何指点!
答案 0 :(得分:0)
不是Word VBA的大人物,但是您可以只比较Start
和End
属性吗?
Dim bm As Bookmark
Set bm = ActiveDocument.Bookmarks("tester")
Debug.Print "Bookmark", bm.Start, bm.End
Debug.Print "Selection", Selection.Start, Selection.End
答案 1 :(得分:0)
要确定一个Range
是否在另一个InRange
方法中,请执行以下操作:
Dim rngBookmark as Word.Range
Dim rngSelection as Word.Range
Set rngBookmark = ActiveDocument.Bookmarks("typeDef").Range
Set rngSelection = Selection.Range
If rngSelection.InRange(rngBookmark) = True Then
'Do something
End If
答案 2 :(得分:0)
您可以使用VBA的InRange方法。例如:
Function FormatSpecHeadReturn(strStyle)
Dim Rng As Range
With ActiveDocument
Set Rng = .Bookmarks("SpecBodyPairRange").Range
With .Bookmarks("SpecBodyPairRange").Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p"
.Replacement.Text = ""
.Style = strStyle
.Format = True
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
.Execute
End With
Do While .Find.Found
If .InRange(Rng) = False Then Exit Do
.Style = "SpecHead"
.Paragraphs.First.InsertBefore Chr(182)
.InsertAfter vbTab
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
End Function