简体   繁体   English

Word VBA-检查光标是否在两个书签之间

[英]Word VBA - check if cursor is between two bookmarks

I am working on Macro in Word (it also uses Excel, but nvm) and I need to check if cursor is currently between two specific bookmarks. 我正在使用Word中的Macro(它也使用Excel,但使用nvm),并且需要检查光标当前是否在两个特定的书签之间。

To be honest I have no idea how to approach the problem. 老实说,我不知道如何解决这个问题。 I used google, I used stackoverflow search and found nothing on this topic. 我用谷歌,我用stackoverflow搜索,但没有找到关于这个主题。 It is my first VBA for Word. 这是我的第一个VBA for Word。

Please understand, that I do not provide any code - as I said, no idea how to try to write it and my research returned no results. 请理解,我没有提供任何代码-正如我所说,不知道如何尝试编写代码,我的研究也未返回任何结果。

The following function will return False or True as a result of checking if the beginning of your selection is between 2 bookmarks passed by name to the function. 由于检查选择的开头是否在按名称传递给函数的2个书签之间,因此以下函数将返回False或True。 It doesn't matter in which order you pass bookmarks name. 传递书签名称的顺序无关紧要。

Function BetweenBookmarks(FirstBookmarkName, SecondBookmarkName)

    If Selection.Start > ActiveDocument.Bookmarks(FirstBookmarkName).Range.End _
        And _
        Selection.End < ActiveDocument.Bookmarks(SecondBookmarkName).Range.Start Then

            BetweenBookmarks = True

    ElseIf Selection.Start > ActiveDocument.Bookmarks(SecondBookmarkName).Range.End _
        And _
        Selection.End < ActiveDocument.Bookmarks(FirstBookmarkName).Range.Start Then

            BetweenBookmarks = True
    Else

            BetweenBookmarks = False
    End If
End Function

Function can be called in this way: 可以通过以下方式调用函数:

Debug.Print BetweenBookmarks("First", "Second")
Debug.Print BetweenBookmarks("Second", "First")

You could make some other modification on your own by changing .End and .Start properties within the function. 您可以通过更改函数中的.End.Start属性自行进行其他修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM