We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

vba script for copying link to selected email in outlook to windows clipboard

mranders
mranders asked
on
Medium Priority
954 Views
Last Modified: 2012-05-06
Is it possible to write a vba script to copy a link to an email in outlook to the clipboard?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
Hi, mranders.

Here's my portion of the code for doing this.  You'll also need the code from this post: http://p2p.wrox.com/access/15100-copy-clipboard.html#post107717  The code from that page copies data to the clipboard.  Follow these instructions to use this.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on Module1
4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window.  Ditto for the code from the page I linked to.
5.  Edit the code as needed.  I included comments wherever something needs to or can change
6.  Click the diskette icon on the toolbar to save the changes
7.  Close the VB Editor

To use this:

1.  Select a message
2.  Run the macro CreateLinkToOutlookItem.  It creates the link and copies it to the clipbard.
3.  Paste the link wherever you want

You can add this as a button on a toolbar by followign these instructions.

1.  Click View->Toolbars->Customize
2.  Click the Toolbars tab
3.  Click New
4.  Name the toolbar
5.  Click the Commands tab
6.  Under Categories click Macros
7.  Under Commands click and hold on the macro, then drag it out and drop it on the new toolbar
8.  Dock the toolbar somewhere onscreen



Sub CreateLinkToOutlookItem()
    Dim olkItem As Outlook.MailItem, strLink As String
    Set olkItem = Application.ActiveExplorer.Selection(1)
    strLink = "outlook://" & olkItem.Parent.Name & "/~" & olkItem.Subject
    ClipBoard_SetData strLink
    Set olkItem = Nothing
End Sub

Open in new window

Author

Commented:
Thanks this looks great! Will try asap.
CERTIFIED EXPERT
Top Expert 2010

Commented:
Cool.

Author

Commented:
Thanks BlueDeilFan! This script in combination with the other rocks! However, for some reason I can't get the produced links to work. They look perfect to me! But, when I try them, this link for example:
outlook://Inbox/~IT
I get the error message shown in the attached file.
Cheers




2009-02-19-112146.jpg
CERTIFIED EXPERT
Top Expert 2010

Commented:
Are you saying that Outlook links don't work at all?  Or is it just links produced by this script, or just this one link?

Author

Commented:
Hi BDF! Any of these links that uses the path format in outlook://Inbox/~IT doesn't work on the two computers I have tested on. One with vista and outlook 2007 and the other with xp and outlook 2003. I don't understand why, because the path looks just fine. The path above for example points to a message with subject "IT" in the inbox, but yields the error dialog attached to my earlier post.
CERTIFIED EXPERT
Top Expert 2010
Commented:
Sorry, I got my folder and item hyperlink format confused.  Try this version instead.
Sub CreateLinkToOutlookItem()
    Dim olkItem As Outlook.MailItem, strLink As String
    Set olkItem = Application.ActiveExplorer.Selection(1)
    strLink = "outlook:" & olkItem.Parent.Name & "/~" & olkItem.Subject
    ClipBoard_SetData strLink
    Set olkItem = Nothing
End Sub

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks, it didn't work on vista and outlook 2007, will try on xp outlook 2003 tomorrow, Cheers
CERTIFIED EXPERT
Top Expert 2010

Commented:
Outlook 2007 doesn't automatically install the URL protocol handler for Outlook items.  You have to edit the registry and add an entry.  Her's a link to a page with details: http://mutable.net/blog/archive/2006/09/02/how_to_hyperlink_to_microsoft_outlook_messages.aspx

Author

Commented:
Interesting! Will try asap. Thanks!

Author

Commented:
It works great now as long as there are no spaces in the subject line, then the link is broken. Anyway I reward the points because this work is really terrific. If you could make it work with the spaces that would be wonderful, but in that case I should open up another question with new points.
CERTIFIED EXPERT
Top Expert 2010

Commented:
No need to open a new question.  Use this version instead.
Sub CreateLinkToOutlookItem()
    Dim olkItem As Outlook.MailItem, strLink As String
    Set olkItem = Application.ActiveExplorer.Selection(1)
    strLink = "outlook:" & olkItem.Parent.Name & "/~" & Replace(olkItem.Subject, " ", "%20")
    ClipBoard_SetData strLink
    Set olkItem = Nothing
End Sub

Open in new window

Author

Commented:
Looks perfect, but for some reason I can't get this to work in neither outlook 2007 nor 2003. Strange.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.