D. Rosen
D. Rosen

Reputation: 3

Applescript - How to get text range of a word in Microsoft Word

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

I am trying to get the text range to convert the word to a link via 'make new hyperlink'.

Upvotes: 0

Views: 907

Answers (1)

wch1zpink
wch1zpink

Reputation: 3142

This works for me using the latest version of Mac OS Sierra

Try this version for using the word number

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

OR

Try this version for using the current selection in the document

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

Upvotes: 1

Related Questions