Search code examples
vbams-wordautohotkeyword-interop

AHK: convert text selection to hyperlink with clipboard as target


Using Autohotkey to achieve this behaviour:

  • copy URL from browser
  • select text in word doc
  • press keyboard shortcut
  • AHK converts selected text to hyperlink with clipboard as target.

This is what I have so far:

^b::
wd := ComObjActive("Word.Application")
url := %clipboard%
wd.ActiveDocument.Hyperlinks.Add(wd.Selection.Range, %url%,"","","")
return

I've tried many other variations, but alway get the error that the url variable contains illegal characters.

Any ideas? Thx!


Solution

  • Variable names in an expression are not enclosed in percent signs.

    ^b::
        wd := ComObjActive("Word.Application")
        url := clipboard
        wd.ActiveDocument.Hyperlinks.Add(wd.Selection.Range, url,"","","")
    return
    

    or

    ^b::
        wd := ComObjActive("Word.Application")
        wd.ActiveDocument.Hyperlinks.Add(wd.Selection.Range, clipboard,"","","")
    return