none
Turn links or text that appear to be links into live links RRS feed

  • Question

  • I have been asked to turn outlook messages that contain links or text that appear to be links, into live links before sending the message.  Outlook I believe does this when sending the email.  What method does Outlook use to search and replace these links?  I'm using visual studio 2008 / Outlook 2007.

    Sunday, September 4, 2011 7:22 AM

Answers

  • Okay, back to this thread.  Thanks first of all for your quick responses Tom.  What I neglected to tell you, was that the resolving of anything that appears to be a link is necessary as the code parses the mail htmlbody and changes some of the links (eg. text to display) before sending the message.  As some users may not have hyperlinks automatically resolved (setting in Outlook), it is necessary to resolve in code prior to parsing.  In reviewing the code you sent me, I'm thinking that there could be issues with users that do not have Word installed.  Also, the code would not handle resolved links properly.

    eg. <a href="http:www.google.com">visit www.google.com</a>

    I'm thinking that it might be better to just use Regex and then loop through the matches as in:

    Dim sPattern as string = "(([A-Za-z]+:\/\/)|(www\.))[a-zA-Z0-9_-]+\.[a-zA-Z0-9-_:%&\?\/.=]+"
    Dim r1 As New Regex(sPattern, RegexOptions.IgnoreCase)
    For Each m As Match In r1.Matches(sHTMLBody)
    .... blah blah
    next
    

    Any thoughts?

    Thanks
     

       

    Sunday, September 18, 2011 8:48 PM
  • Precisely.  Within the loop, I would scan for the hyperlink tag (begin and end tag).

    Thanks

    Tuesday, September 20, 2011 8:40 PM

All replies

  • Hi J,

     

    Thanks for posting in the MSDN Forum.

     

    Do you want to the string in your body which begin with http:// became a hyperlink automatic?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 5, 2011 10:46 AM
    Moderator
  • I would like to resolve links the same way outlook turns message text into fully qualified links.  This would include the following:

    eg.  http://  or https://  or ftp://  or  mailto:  or www.google.com (for example)

    Basically, anything that appears to be a link. 


    • Edited by J Wagner Monday, September 5, 2011 5:33 PM
    Monday, September 5, 2011 5:32 PM
  • Hi J,

     

    As far as I known, In Outlook 2007 will change the string to hyperlink automate if the string begin with http://, https://, ftp:// or mailto: . So would you please provide me the specific address string which will not change to hyperlink via Outlook for further research?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 6, 2011 7:31 AM
    Moderator
  • There is not a problem with Outlook, I just want to know what method (CODE) outlook uses to scan a message and change links to hyperlinks.  It does not just go after http:, ftp or mailto, as this may not be present at the time of entering the link, for example, I can enter www.google.ca and the htmlbody will just show " www.google.ca " or ">www.google.ca<" etc.  My requirements are to change anything that appears to be a link, to a hyperlink, before actually sending the message.
    Tuesday, September 6, 2011 4:11 PM
  • Hi J,

     

    I reproduce this issue via following code. I think this snippet is the way we use to reproduce your goal. Maybe the Inspector will fire an event to do the same things when you type your content in to the edit area.

     

            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                try
                {
                    Outlook.Application olApplication = Globals.ThisAddIn.Application;
                    Outlook.Inspector olInspector = olApplication.ActiveInspector();
                    Word.Document doc = olInspector.WordEditor as Word.Document;
                    if (doc != null)
                    {
                        Word.Range range = doc.Range();
                        string str = range.Text;
                        Regex r = new Regex("\r");
                        str = r.Replace(str, " ");
                        string[] srcs = str.Split(' ');
                        Regex reg = new Regex(@"www.+", RegexOptions.IgnoreCase);
                        for (int i = 0; i < srcs.Length; i++)
                        {
                            if (reg.IsMatch(srcs[i]))
                            {
                                object t = srcs[i];
                                object u = "http://" + srcs[i];
                                object m = Type.Missing;
                                range.Find.Text = srcs[i];
                                range.Find.Execute();
                                range.Hyperlinks.Add(range, ref u, ref m, ref m, ref t, ref m);
                                range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    

     

    By the way, this is the snippet only to reproduce your issue. I think Outlook will handle this on other ways.

     

    Have a good day,

     

    Tom

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 7, 2011 5:51 AM
    Moderator
  • Tom, thank you for your responses.  In looking at the code, it appears to only go after the 'www.' links.   If I enter https://support.blacklight, outlook resolves this link.  This site does as well.  The code you sent me would ignore this link.  Is that correct?
    Wednesday, September 7, 2011 3:20 PM
  • Hi J,

     

    The snippet which I show you only use the key word www. You can add the http:// ftp:// and mailto by yourself. We use this snippet to change the specific key work which will not be recognized via Outlook or Word to a specific hyperlink with specific url address.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 8, 2011 8:19 AM
    Moderator
  • Okay, back to this thread.  Thanks first of all for your quick responses Tom.  What I neglected to tell you, was that the resolving of anything that appears to be a link is necessary as the code parses the mail htmlbody and changes some of the links (eg. text to display) before sending the message.  As some users may not have hyperlinks automatically resolved (setting in Outlook), it is necessary to resolve in code prior to parsing.  In reviewing the code you sent me, I'm thinking that there could be issues with users that do not have Word installed.  Also, the code would not handle resolved links properly.

    eg. <a href="http:www.google.com">visit www.google.com</a>

    I'm thinking that it might be better to just use Regex and then loop through the matches as in:

    Dim sPattern as string = "(([A-Za-z]+:\/\/)|(www\.))[a-zA-Z0-9_-]+\.[a-zA-Z0-9-_:%&\?\/.=]+"
    Dim r1 As New Regex(sPattern, RegexOptions.IgnoreCase)
    For Each m As Match In r1.Matches(sHTMLBody)
    .... blah blah
    next
    

    Any thoughts?

    Thanks
     

       

    Sunday, September 18, 2011 8:48 PM
  • Hi J,

     

    The snippet you show me is OK for this issue. But we must ensure there are no hyperlink exist in the Body content already.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 20, 2011 7:46 AM
    Moderator
  • Precisely.  Within the loop, I would scan for the hyperlink tag (begin and end tag).

    Thanks

    Tuesday, September 20, 2011 8:40 PM