Applescript - 如何在Microsoft Word中获取单词的文本范围

时间:2018-03-09 15:46:31

标签: ms-word applescript

E.g。

tell application "Microsoft Word"
    tell active document
        set theRange to text range of word 5 of content of text object
        ...
     end tell
end tell

我正在尝试通过“制作新的超链接”将文字范围转换为链接。

1 个答案:

答案 0 :(得分:1)

这适用于我使用最新版本的Mac OS Sierra

尝试使用此版本以使用单词编号

chosenWord(5)

on chosenWord(wordNumber)
    tell application "Microsoft Word"
        tell its document 1
            set theWord to content of word wordNumber
            tell its word wordNumber
                set {theStartOfRange, theEndOfRange} to {start of content, end of content}
                set theRange to ("The Character Range Is " & (theStartOfRange as text) & " thru " & (theEndOfRange as text))
            end tell
        end tell
    end tell
end chosenWord

尝试使用此版本以使用文档中的当前选择

tell application "Microsoft Word"
    set theSelection to content of words of selection
    set {selectionStart, selectionEnd} to ¬
        {selection start of selection, selection end of selection}
    set theRange to selectionStart & selectionEnd
end tell
return theRange