More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

Login

login
password
Remember me

You will need to register and log in if you want to download the source code for the Microsoft Outlook Programming book. The forums and code sharing areas are open to both registered and non-registered visitors.

share code 06-Sep-2010 21:36

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

 

Code level: intermediate    Code area: Basic Outlook Printer Friendly Version
Title: Insert hyperlink in Outlook 2007 items
Description: Because all Outlook 2007 items other than sticky notes use Word as the editor, you can use Word objects to insert a hyperlink.
Date: 12-Nov-2007  08:34
Code level: intermediate
Code area: Basic Outlook
Posted by: Sue Mosher
Body:
All 34comments
Page [ 1 2 3 4 Next >>  
  12-Nov-2007  08:39   
Notes on the code:

1) This code inserts text at the insertion point, in other words, where the cursor appears in the item body. To insert it elsewhere, you would use Word methods to return a different Range object for use with the first parameter of the Hyperlinks.Add method.

2) To try it out, add the code to any Outlook VBA module, open a new email message, then execute code statement in the VBA Immediate windor or from a macro:

    InsertLink ActiveInspector.CurrentItem

3) You must use this method if you want to add an outlook: link to any task, calendar, journal, or contact body.

4) For more on working with Word objects in the context of Outlook item bodies, see Chapter 17 in my Microsoft Outlook 2007 Programming book, from which this example is taken.
  29-Nov-2007  04:43   
How can i add a hyper link to an executable file, where the path contains spaces? The code i am using creates a valid hyperlink up to the 1st space, and then the rest of the path is just plain text. Please remove nospam from email address.
  29-Nov-2007  07:59   
Use the Replace() function to spaces in the hyperlink string with "%20" which is the HTML equivalent of a space.
  07-Dec-2007  13:56   
I'm trying to add some text into mails using Outlook 2007. Seems Word document is read-only protected, so first I have to unprotect it.

doc.Unprotect( ref missing );
doc.Content.InsertBefore( "This is a test" );

This works as expected for html mail, but doesn't work for plain text mail. When accessing plain text mail, the doc.Unprotect method throws an COMException with "This command is not available." as exception's message. If I don't call the Unprotect method than the doc.InsertBefore method throws an COMException with "This method or property is not available because the document is locked for editing."
Any ideas?
  07-Dec-2007  14:09   
Boris, are you working with a new unsent message or a received message? Are you returning the Word.Document using the code above?
  07-Dec-2007  14:52   
Hi!

Tnx for quick response.

I'm working with received messages, when they are displayed within inspector or preview pane. I don't want to change MailItem (for example, if you have an IMAP account, I don't want to synchronize mails with my text appended with IMAP mail server)

I append text within the Outlook.Explorer's SelectionChange event. Changing msg.GetInspector.WordEditor Word.Document doesn't seem to reflect changes within preview pane, so I locate the window containing the mail (that is the ligh-weight word control), call the AccessibleObjectFromWindow to get Word.Document form HWND and than do the magic :)

As said before, it works perfectly with HTML mail.

Tnx in advance,
 Boris
  07-Dec-2007  15:34   
Boris, there is no supported way to change the display of the reading pane without changing the underlying item, which means calling MailItem.Save. If you've found a hack, maybe you could share it by posting a complete code sample in the "share code" section of the site at http://www.outlookcode.com/code.aspx
  07-Dec-2007  16:58   
That is what I was afraid of. When (and if) I find a way, I will definitely share my solution here. Thx for your help,
 Boris

P.S. I', really appreciate your prompt responses. Hope to deal with that kind of support soon. Cheers.
  10-Dec-2007  03:40   
Hi!

I'm unable to run your sample. When I try to run the code above, I got the "Invalid selection" message box and within code, I got a COMException with "Out of memory" message and error code 0x800a1007. If I change the code to:

string strLink = "http://www.outlookcode.com";
string strLinkText = "Get Outlook code samples here";
Outlook.Inspector objInsp = mi.GetInspector;
Word.Document objDoc = objInsp.WordEditor as Word.Document;
if ( mi.BodyFormat != Outlook.OlBodyFormat.olFormatPlain )
{
    object ostrLink = strLink;
    object oempty = string.Empty;
    object ostrLinkText = strLinkText;
    objDoc.Hyperlinks.Add( objDoc.Content, ref ostrLink, ref oempty, ref oempty, ref ostrLinkText, ref oempty );
}
else
    objDoc.Content.InsertAfter( strLink );

I get a COMException "Command failed."

But when I simplify the code to

Outlook.Inspector objInsp = mi.GetInspector;
Word.Document objDoc = objInsp.WordEditor as Word.Document;
objDoc.Content.InsertAfter( "This is a test! " );

I get a COMException with "This method or property is not available because the document is locked for editing.".

All exceptions are raised when trying to modify the Word document (insert a hyper link, AddAfter).

The code is executed within the OnExplorerSelectionChange event.

If I add the following snipped before modifying the mailitem, than it works for HTML mail items!

object missing = System.Reflection.Missing.Value;
Word.WdProtectionType protectionType = objDoc.ProtectionType;
if ( protectionType != Word.WdProtectionType.wdNoProtection )
    objDoc.Unprotect( ref missing );

For plain text mail, the Unprotect method throws a COMException "This command is not available".

I'm working with received mail. Is there a way to around this issue??? I mean, I simply cannot find a reason why the same code works for HTML mail, but does not for plain text mail. It seems both use Word.Document, but lack the ability to unprotect the document. Any help would be greatly appreciated.
  19-Jun-2008  06:07   
Thanks very much for this. An excellent bit of coding and much appreciated.
Page [ 1 2 3 4 Next >>  
Post your comment



name        email