Word COM - Insert hyperlink (email address) into Word bookmark Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Word COM - Insert hyperlink (email address) into Word bookmark

Post by jarhead » 18 Jan 2022, 14:50

I have a script that prompts a user for information and then inserts the information into a Word document via bookmarks, exports it to a PDF and then creates a new Outlook message and attaches the PDF which is working great.

My only issue is inserting a linkable email address (hyperlink) into one of the bookmarks. My Gui has a DropDownList control that provides name choices. Using Switch, when the name is chosen, it also has other information including email address. My Switch statements look like this:
case "Bob Smith": ManagerName := "Robert J. Smith", ManagerEmail := "[email protected]"
In my Word document, the bookmark I'm attempting to insert the hyperlink into is as follows:

Code: Select all

MyDocNew.Bookmarks("CMemail").Range.Text := ManagerEmail
The email address is passed correctly to the Word bookmark; however, it is not a hyperlink.

I've reviewed Hyperlinks.Add but not sure how to make it work or how this is modified to work with AHK. I tried using mailto: in various ways as well. Thanks!
Last edited by jarhead on 19 Jan 2022, 09:26, edited 1 time in total.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Word COM - Insert hyperlink (email address) into Word bookmark from Gui dropdown box

Post by flyingDman » 18 Jan 2022, 15:00

Try:

Code: Select all

oWord := ComObjActive("Word.Application")
ManagerEmail := "[email protected]"
oWord.ActiveDocument.Hyperlinks.Add(oWord.Selection.Range, ManagerEmail)
or oWord.ActiveDocument.Hyperlinks.Add(oWord.Selection.Range, ManagerEmail, , , "John")
14.3 & 1.3.7

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Word COM - Insert hyperlink (email address) into Word bookmark from Gui dropdown box

Post by jarhead » 18 Jan 2022, 15:44

Thanks flyingDman... that works for inserting into an active Word doc and this will be very useful. How would I insert it into a Word bookmark? I've tried different variations and it seems like something like this would work: MyDocNew.Hyperlinks.Add(MyDocNew.Bookmarks("CMemail"), ManagerEmail)

In my script, I'm using wdApp := ComObjCreate("Word.Application") and MyDocNew := wdApp.Documents.Add(FilePath)

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Word COM - Insert hyperlink (email address) into Word bookmark from Gui dropdown box

Post by jarhead » 19 Jan 2022, 08:48

I found this VBA script for inserting a hyperlink at a Word bookmark:

Code: Select all

with WrdApp.ActiveDocument
.Hyperlinks.Add Anchor:=.Bookmarks(BookmarkName).Range, _
Adress:=MyWebAdd, _
TextToDisplay:=MyWebAdd
End With
I've tried various scripts like this → MyDocNew.Hyperlinks.Add(MyDocNew.Bookmarks("CMemail").Range, ManagerEmail) and get type mismatch errors or "the requested member of the collection does not exist" errors.

Any ideas?

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Word COM - Insert hyperlink (email address) into Word bookmark  Topic is solved

Post by jarhead » 19 Jan 2022, 12:28

Not sure if this is the correct way but it works. It just required adding .Item after MyDocNew.Bookmarks.

Working script

Code: Select all

MyDocNew.Hyperlinks.Add(MyDocNew.Bookmarks.Item("CMemail").Range, ManagerEmail)

Post Reply

Return to “Ask for Help (v1)”