locked
Creating a hyperlink programmatically from a word addin causes out of memory error RRS feed

  • Question

  • Hi,

    I have a problem when creating a hyperlink programmatically from a word addin.
    If the user clicked on a content control where the place holder text gets selected i get
    an error saying "out of memory". If i look closer into the exception the HResult=-2146824185
    I'm not entirely sure what the error code means, maybe you have some idea of what is the cause of the problem?

    Here is a code snippet

    IDocumentBC bc = DocumentBC;
    var documentLink = bc.CreateDocumentLink(pIndex);
    Range currentRange = Application.Selection.Range;

    if (currentRange != null)
    {
        Editors e = currentRange.Editors;
        int numberOfEditableRegions = e.Count;

        //We need to check the users insert point(could be within a protected area of the document)
        //so that we are able to insert the link before we actually create it.
        if (numberOfEditableRegions > 0)
        {
            try
            {
                //By default, the hyperlink TextToDisplay will be set to the index text
                //currentRange.Hyperlinks.Add(currentRange, serviceUri + "document/" + mTxtIndexLink);
                Hyperlink h = currentRange.Hyperlinks.Add(
                    currentRange,
                    documentLink,
                    Type.Missing,
                    "Öppna dokument",
                    pIndex);
            }
            catch (Exception ex)
            {
                //If the user selected a content control where the place holder text gets selected, an exception is thrown where HResult=-2146824185

                MessageBox.Show(
                    "Det gick inte att skapa länk till dokumentet. Detta kan bero på att du markerat en innehållskontroll som ännu inte har någon information.\n\n" + ex.HResult);
            }

        }
        else
        {
            MessageBox.Show("Du har placerat markören på en plats i dokumentet som inte är redigerbar, flytta markören och försök igen.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }


    Creating a link works as expected in all other cases.

    Br,
    /Peter


    Peter

    Wednesday, October 1, 2014 1:06 PM

Answers

  • Hi,

    After running your code (I remove some unrelated part in the code), I think I can reproduce your issue when I insert a rich text content control into the document as the picture below and then run my code.

    After testing, I think the exception is caused by that no text/range is in the content control after inserting. If we input some characters into the content control, then the code can run well without exception.

    So for a workaround, I think we can set the Text property of Range object "currentRange" with a space string " " before adding hyperlink. You can refer to the code below. In this way, after adding hyperlink, the space will be replaced by the text of hyperlink.

    private void TestHyperLink()
    {
        //IDocumentBC bc = DocumentBC;
        var documentLink = "www.bing.com"; //bc.CreateDocumentLink(pIndex);
        Word.Range currentRange = app.Selection.Range; //app means the active Word application
    
        if (currentRange != null)
        {
            try
            {
                //By default, the hyperlink TextToDisplay will be set to the index text
                //currentRange.Hyperlinks.Add(currentRange, serviceUri + "document/" + mTxtIndexLink);
                currentRange.Text = " ";
                Word.Hyperlink h = currentRange.Hyperlinks.Add(
                    currentRange,
                    documentLink,
                    Type.Missing,
                    "Öppna dokument",
                    "abc");
            }
            catch (Exception ex)
            {
                //If the user selected a content control where the place holder text gets selected, an exception is thrown where HResult=-2146824185
    
                MessageBox.Show(
                        "Det gick inte att skapa länk till dokumentet. Detta kan bero på att du markerat en innehållskontroll som ännu inte har någon information.\n\n" + ex.HResult);
            }
        }
    
    }


    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.

    Thursday, October 2, 2014 2:46 AM
  • Hi Peter,

    I was able to reproduce your issue at my end. It looks like the issue is a bug in Word 2013. It has been reported to the product group. 

    Thanks,

    Anush Rudaa

    Wednesday, October 15, 2014 11:29 PM

All replies

  • Hi,

    After running your code (I remove some unrelated part in the code), I think I can reproduce your issue when I insert a rich text content control into the document as the picture below and then run my code.

    After testing, I think the exception is caused by that no text/range is in the content control after inserting. If we input some characters into the content control, then the code can run well without exception.

    So for a workaround, I think we can set the Text property of Range object "currentRange" with a space string " " before adding hyperlink. You can refer to the code below. In this way, after adding hyperlink, the space will be replaced by the text of hyperlink.

    private void TestHyperLink()
    {
        //IDocumentBC bc = DocumentBC;
        var documentLink = "www.bing.com"; //bc.CreateDocumentLink(pIndex);
        Word.Range currentRange = app.Selection.Range; //app means the active Word application
    
        if (currentRange != null)
        {
            try
            {
                //By default, the hyperlink TextToDisplay will be set to the index text
                //currentRange.Hyperlinks.Add(currentRange, serviceUri + "document/" + mTxtIndexLink);
                currentRange.Text = " ";
                Word.Hyperlink h = currentRange.Hyperlinks.Add(
                    currentRange,
                    documentLink,
                    Type.Missing,
                    "Öppna dokument",
                    "abc");
            }
            catch (Exception ex)
            {
                //If the user selected a content control where the place holder text gets selected, an exception is thrown where HResult=-2146824185
    
                MessageBox.Show(
                        "Det gick inte att skapa länk till dokumentet. Detta kan bero på att du markerat en innehållskontroll som ännu inte har någon information.\n\n" + ex.HResult);
            }
        }
    
    }


    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.

    Thursday, October 2, 2014 2:46 AM
  • Hi,

    Thanks for your reply and your workaround. I have implemented a similar solution :)

    However, i was curious about why the error occurs in the first place. If i for example mark a

    regular word within a text, the hyperlink inserted replaces the text. I would expect the same behaviour

    in this case.  Anyway, just my thoughts, i'm begining to get used to all workarounds :)

    Thanks again

    Br,

    /Peter


    Peter

    Thursday, October 2, 2014 7:06 AM
  • Hi,

    >>i was curious about why the error occurs in the first place. <<

    I am trying to involve someone familiar with this topic to further look at that. There might be some time delay. Appreciate your patience.

    Sorry for any inconvenience and have a nice day!

    Best regards.

    Luna


    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, October 7, 2014 9:28 AM
  • Hi Peter,

    I was able to reproduce your issue at my end. It looks like the issue is a bug in Word 2013. It has been reported to the product group. 

    Thanks,

    Anush Rudaa

    Wednesday, October 15, 2014 11:29 PM