Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 940
  • Last Modified:

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

Is it possible to write a vba script to copy a link to an email in outlook to the clipboard?
0
mranders
Asked:
mranders
  • 7
  • 6
1 Solution
 
David LeeCommented:
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

0
 
mrandersAuthor Commented:
Thanks this looks great! Will try asap.
0
 
David LeeCommented:
Cool.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrandersAuthor 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
0
 
David LeeCommented:
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?
0
 
mrandersAuthor 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.
0
 
David LeeCommented:
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

0
 
mrandersAuthor Commented:
Thanks, it didn't work on vista and outlook 2007, will try on xp outlook 2003 tomorrow, Cheers
0
 
David LeeCommented:
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
0
 
mrandersAuthor Commented:
Interesting! Will try asap. Thanks!
0
 
mrandersAuthor 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.
0
 
David LeeCommented:
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

0
 
mrandersAuthor Commented:
Looks perfect, but for some reason I can't get this to work in neither outlook 2007 nor 2003. Strange.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now