Today I wrote a mail to someone and wanted to add several links. It always annoyed me that Office’s Add Hyperlink is so bloated it takes some time to open, and even more time to switch back to it from another application, where all I wanted was to make the selected text link to the URL I had in my clipboard. So I set out to write a macro that did it.
Since Outlook has no macro recorder, I started searching Outlook’s Object Browser and discovered that there is no method to add a hyperlink or to get the content of the clipboard. So I borrowed the “Getting clipboard content” functionality from a VBS script I already had, and I turned to Word to record a macro that adds a hyperlink to the selected text. Once I had this Word macro, I found out I can use ActiveInspector.WordEditor to issue the Word macro I created.
The final macro result is this:
Public Sub AddLinkFromClipboard() If ActiveInspector.EditorType = olEditorText Then MsgBox "Can't add links to textual mail" Exit Sub End If Dim objHTM As Object Dim ClipboardData As String Dim doc As Object Dim sel As Object Set objHTM = CreateObject("htmlfile") ClipboardData = objHTM.ParentWindow.ClipboardData.GetData("text") Set doc = ActiveInspector.WordEditor Set sel = doc.Application.Selection doc.Hyperlinks.Add Anchor:=sel.Range, _ Address:=ClipboardData, _ SubAddress:="", _ ScreenTip:="", _ TextToDisplay:=sel.Text End Sub
June 26, 2008 at 8:57 am |
Hi There,
You can put sourcecode in tags like so
and it will display nicely on wordpress.
Cheers
June 26, 2008 at 8:58 am |
I wasn’t expecting that, it even works in comments.
Here is a link to how to do it.
June 26, 2008 at 9:24 am |
@Alister: Thank you so much. I updated the post.
November 25, 2008 at 6:51 am |
Awesome….. thanx for the same….
December 10, 2014 at 5:47 pm |
this really works!! Are you looking to paste whats in your clipboard to anywhere in your email body as a hyperlink? HERES THE SOLUTION!!
October 17, 2015 at 6:48 am |
I can get the code to run, and turn text into a hyperlink using the link address was on the clip board, but unless I right click and the click “Edit hyperlink”, and then simply click “ok” the link does not work i.e. I have to open up and then click “OK” to the Edit hyperlink dialog box before sending the email or else the link will not work – does anyone have a fix for this? Any help much appreciated.
October 17, 2015 at 6:55 am |
Sorry, I should have said, the problem referred to in the comment above only occurs when the link is to a document path on C:, or the server, and this problem does not occur when the link is to a website URL. Thanks again.
June 30, 2016 at 1:28 am |
This is exactly what I’ve been looking for. At work, when providing tracking information, I’ve been typing “(link)” and then manually selecting the word “link” and using ctrl+k to open the hyperlink and paste the hyperlink from there. It doesn’t sound like much, but I wanted a way to automate this just how you approached it.
The only chance I made was that I added…
sel.TypeText Text:=”(link)”
sel.MoveLeft Unit:=wdCharacter, Count:=1
sel.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
…between “Set sel = doc.Application.Selection” & “doc.Hyperlinks.add Anchor:=sel.Range, _”
in order to to type in “(link)” and then select just “link”..
July 1, 2016 at 8:47 pm |
Correction/Update…I added an IF statement to account for whether the cursor is selecting text already or not. If it is not selecting anything, it adds “(link)” with only the word link as the hyperlinked text…if something is selected, then the selection is used for the hyperlink text.
If Len(sel.Text) = 1 Then
sel.TypeText Text:=”(link)”
sel.MoveLeft Unit:=wdCharacter, Count:=1
sel.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
sel.Text = “link”
End If
*hint* if you copy/paste the code here, the quotation marks will throw an error in the compiler so you have to delete and re-type them (or at least I had to).