none
Finding the end of the text in a RichTextContentControl RRS feed

  • Question

  • Hi Everyone,

    I am building an Application-level add-in for Word 2010 using C#.

    I am inserting AutoText into a RichTextContentControl.  Using this code:

     

                            // Get a reference to the auto text
                            bb = tplAbbrv.BuildingBlockTypes.Item(paramBBType).Categories.Item(ref paramBBCategory).BuildingBlocks.Item(ref paramBBName);

                            // Position the cursor at the end of the range object
                            rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);

                            // Expand the auto text
                            bb.Insert(rng, ref paramMissing);

    Where bb is a reference to the BuildingBlock, and rng is a reference to the Range of the ContentControl.

    The code works fine, until I insert a second AutoText entry into the same content control.  I want the second entry to follow the first entry, but instead, the second entry is appearing first.  

    I imagine that the problem has to do with a misunderstanding of ranges and how to position cursor at the end of a range.  I had thought that collapsing the range to the end would do the trick, but it's not working.

    Any response would be appreciated!!  Thanks in advance for your help.


    Friday, January 18, 2013 10:28 PM

Answers

  • Hi Susan

    <<Given that the range is a rich text content control, how do I "Get the Range again, this time including the just pasted AutoText?">>

    The same way you do when you get it the first time. Something like:
      Range = theContentControl.Range

    If you are doing that, then check whether the Range is still IN the content control after you've collapse it. You can do this by using Range.Select then ending your code and seeing where the insertion point is blinking and if the content control is visible. If not, then collapsing the Range is putting it outside the content control and you need to Range.MoveEnd by -1 unit of the type wdCharacter


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 4:55 PM
    Moderator

All replies

  • Hi Susan

    Since you don't show us the complete code we can only guess, but...

    When you insert an AutoText entry into a "target" Range usually the Range does NOT expand to include the AutoText, it remains before the AutoText. So try getting the Range rng again from the content control after inserting the AutoText, then collapsing it to its end-point.


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, January 20, 2013 8:08 AM
    Moderator
  • Hi Cindy,

    Thank you for your response.  Below is the method I wrote for inserting AutoText into a range.  I collapse the range to the end before inserting the AutoText.  Doesn't collapsing to the end of the range position the cursor at the end of the range?  If not, how do I go about getting the end of the range?

    Thank you for your help!

    public void ExpandAbbreviation(Word.Range rng, string abbrvCode)
    {
        Word.Application WordApp = Globals.ThisAddIn.Application;
        Word.Template tplAbbrv = null;
        Word.BuildingBlock bb;
        Word.WdBuildingBlockTypes paramBBType = Word.WdBuildingBlockTypes.wdTypeAutoText;
        object paramBBCategory = "Abbreviations";
        object paramBBName = abbrvCode;
        object paramMissing = Type.Missing;

        try
        {
    // Get the name of the abbreviations template file from the project properties
    string[] filePathName = Properties.Settings.Default.AbbrvFile.Split('\\');
    if (filePathName.Length > 1)
    {
        string fileName = filePathName[filePathName.Length - 1];

        WordApp.Templates.LoadBuildingBlocks();

        Word.Templates tpls = WordApp.Templates;
        foreach (Word.Template wt in tpls)
        {
    if (wt.Name == fileName)
        tplAbbrv = wt;

        };

        // Expand the abbreviation at the selection
        if (tplAbbrv != null && paramBBName.ToString().Substring(1, 1) != "-")
        {
    // Get a reference to the auto text
    bb = tplAbbrv.BuildingBlockTypes.Item(paramBBType).Categories.Item(ref paramBBCategory).BuildingBlocks.Item(ref paramBBName);

    // Position the cursor at the end of the range object
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
     
    // Expand the auto text
    bb.Insert(rng, ref paramMissing);
        }
    }
        }
        catch (Exception ex)
        {
    MessageBox.Show("The abbreviation " + paramBBName + " could not be found in the Abbreviations file.\n(" + ex.Message+ ")");
        }
    }

    Tuesday, January 22, 2013 4:34 PM
  • Hi Susan

    <<Doesn't collapsing to the end of the range position the cursor at the end of the range?>>

    It does, but if inserting the AutoText doesn't put the AutoText into that range (the Range.End precedes the start of the inserted AutoText), then the next time you insert an AutoText it will go in front of the one inserted previously.

    You only show us collapsing the Range, you don't show us how you define the range before calling this method. If you do something like:

      Get the Range
      Call ExpandAbbreviation
      Call ExpandAbbreviation

    Then Range will remain before the content of each AutoText entry. You'd need to do this

      Get the Range
      Call ExpandAbbreviation
      Get the Range again, this time including the just pasted AutoText
      Call ExpandAbbreviation


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, January 22, 2013 7:21 PM
    Moderator
  • Hi Cindy,

    I am greatful for your assistance.  I have learned a lot from you over the last year or so!!

    I am doing as you described:

    Getting the range  (which is a reference to a ContentControl's range.)
    calling ExpandAbbreviation, and then
    calling ExpandAbbreviation again.

    With in ExpandAbbreviation, I am collapsing the range to the end just prior to inserting the AutoText.  So why doesn't the AutoText get inserted at the end of the range?

    Given that the range is a rich text content control, how do I "Get the Range again, this time including the just pasted AutoText?"

    Thanks again for your help.

    Wednesday, January 23, 2013 3:47 PM
  • Hi Susan

    <<Given that the range is a rich text content control, how do I "Get the Range again, this time including the just pasted AutoText?">>

    The same way you do when you get it the first time. Something like:
      Range = theContentControl.Range

    If you are doing that, then check whether the Range is still IN the content control after you've collapse it. You can do this by using Range.Select then ending your code and seeing where the insertion point is blinking and if the content control is visible. If not, then collapsing the Range is putting it outside the content control and you need to Range.MoveEnd by -1 unit of the type wdCharacter


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 4:55 PM
    Moderator