none
Try-Catch method for hyperlinks RRS feed

  • Question

  • Hi All,

    I have the following method to test the validity of hyperlinks.The method works just fine. But if more than ten hyperlinks are present in the document then my word just crashes and gets stuck. Please let me know an alternate method that validates hyperlinks.

    Another problem is , it also evaluates links inside my table of contents, how to omit that?

       Microsoft.Office.Interop.Word.Hyperlinks links = oDoc.Hyperlinks;
               // MessageBox.Show(""+links.Count);
                foreach (Word.Hyperlink link in links)
                {
                    //MessageBox.Show("" + link.Address);
                  
                    
                    try
                    {
                        WebRequest webRequest = WebRequest.Create(link.Address);
                        WebResponse webResponse;
                        webResponse = webRequest.GetResponse();//time consuming
                    
                    }
                    catch //If exception thrown, then couldn't get response from address
                    {
                        MessageBox.Show("Invalid Link"+link.Address);
                        
                    }
                  
               
                }

    Any help is much much appreciated.Thanks in Advance!

    Thursday, November 6, 2014 9:12 AM

Answers

  • Hi Apoorva,

    For this issue, I think the reason is that it will takes too much time to validate the hyperlinks which will lead the current program crush.

    I suggest that you could do the hyperlink validate in the other thread.

    For the other issue, I suggest that you should create a new thread.

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 7, 2014 10:00 AM
    Moderator
  • Hi Apoorva,

    Using Thread class to start a new thread.

    More information, please refer to:

    # Threading Tutorial

    http://msdn.microsoft.com/en-in/library/aa645740(v=vs.71).aspx

    To notify the result to main thread, I think you could use an ActionControlPanel (could hidden it) to call Invoke method.

    More information, please refer to:

    # Control.Invoke Method (Delegate)

    http://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 11, 2014 7:36 AM
    Moderator

All replies

  • Hi Apoorva,

    For this issue, I think the reason is that it will takes too much time to validate the hyperlinks which will lead the current program crush.

    I suggest that you could do the hyperlink validate in the other thread.

    For the other issue, I suggest that you should create a new thread.

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 7, 2014 10:00 AM
    Moderator
  • Thanks Starain, I will try that method, but I found that using this line helped me to  great extent,just found this

    WebRequest webRequest = WebRequest.Create(link.Address);
                        webRequest.Proxy = null;
                        using (var response = (HttpWebResponse)webRequest.GetResponse());

    Friday, November 7, 2014 12:33 PM
  • Hi Apoorva,

    Your code may reduce the time of request to throw exception, in my opinion, since they are taken too much to check the hyperlinks, it’s better do it in other thread, otherwise, it may block the main thread (can’t do anything), even lead the application crash.

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, November 10, 2014 10:01 AM
    Moderator
  • Thanks Starain, can you please elaborate as to what another thread here means cause I'am not very clear can you please show me a snippet may be a small sample that can solve this issue  ? 
    Monday, November 10, 2014 3:09 PM
  • Hi Apoorva,

    Using Thread class to start a new thread.

    More information, please refer to:

    # Threading Tutorial

    http://msdn.microsoft.com/en-in/library/aa645740(v=vs.71).aspx

    To notify the result to main thread, I think you could use an ActionControlPanel (could hidden it) to call Invoke method.

    More information, please refer to:

    # Control.Invoke Method (Delegate)

    http://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 11, 2014 7:36 AM
    Moderator
  • Thanks a lot Starain,

    I will read upon on these and try to implement this, in my code.Thank you :) 

    Tuesday, November 11, 2014 7:48 AM
  • This method certainly works very well Starain.Thank you so much ,but just one doubt, if a thread is started , how to display a wait message kind of a thing , in ms word? 
    Wednesday, November 12, 2014 8:48 AM