none
Getting and Setting the content/formatting for a rich text content control RRS feed

  • Question

  • Hello, 

      I have a Word add-in that is getting a header as well as a table and rich text content control from a server when the document loads. The add-in has functionality such that when a button is pressed the header and all downloaded content from the server is refreshed. However, if the user has formatted text in the rich text content controller it is wiped due to the refresh. I have been able to use a getter and setter to get the text in the RTCC and insert it after the refresh, but it loses all of its formatting. 

    Is there a way to do this with a content control? I have tried doing something similar to what I was doing with the text of the RTCC but the public content control variable always seems to null out when the setter is called.

    Here is my code:

    In another class I am calling

    public static ContentControl temp = documentUtility.SetRichTextContentControl(bookmark);

    ...

    documentUtility.GetRichTextContentControl(trexContent, temp);

    then, the get and set in the same class:

      public static ContentControl tempCC;

    ...

           public ContentControl SetRichTextContentControl(Bookmark bookmark)
            {
                foreach (ContentControl cc in bookmark.Range.ContentControls)
                {
                    if (cc.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
                    {
                        tempCC = cc;
                    }
                    return tempCC;
                }
                return null;
            }

       public void GetRichTextContentControl(Bookmark bookmark, ContentControl rtcc)
            {
                foreach (ContentControl cc in bookmark.Range.ContentControls)
                {
                    if (cc.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
                    {
                        if (cc.Range.Text == "Click here to enter text." && rtcc != null)
                        {
                            // Here I want to assign cc = rtcc but you cant do that to a var in a foreach loop
                        }
                    }
                }

            }

    rtcc gets passed through as null here as well. Is there a better way to do this?

    • Edited by Luke415 Tuesday, October 27, 2015 9:18 PM Adding code
    Tuesday, October 27, 2015 9:01 PM

Answers

  • Hi Luke

    It's also not clear what you expect. You refresh the RT content control - with what? New content (i.e. different text)? Then how is the formatting applied previously relevant when the content has changed?

    If you mean you're trying to save the formatted text content, then I think the only way that's possible is to save the CC.Range.WordOpenXML (note that you may first need to change the exact Range start and end points so that you get only the content and not the control), then write that back using CC.Range.InsertXML.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Wednesday, October 28, 2015 6:38 PM
    Moderator

All replies

  • Hi,

    Base on my check ContentControl object, it doesn’t have the property or method to set or get the formatting, I also did the text with Range.Style, but it doesn’t work.

    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.

    Wednesday, October 28, 2015 8:39 AM
    Moderator
  • Hi Luke

    It's also not clear what you expect. You refresh the RT content control - with what? New content (i.e. different text)? Then how is the formatting applied previously relevant when the content has changed?

    If you mean you're trying to save the formatted text content, then I think the only way that's possible is to save the CC.Range.WordOpenXML (note that you may first need to change the exact Range start and end points so that you get only the content and not the control), then write that back using CC.Range.InsertXML.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Wednesday, October 28, 2015 6:38 PM
    Moderator
  • I need to refresh the document with new content but want to keep the RTCC text and formatting the same.

    What I ended up doing was copying the RTCC to another bookmark, refreshing the rest of the doc, and then copying the RTCC back into the original bookmark.

    Friday, October 30, 2015 3:30 PM
  • It seems you want to preserve the formatted content when the underlying text remains the same through a refresh. In that case, you might do better to compare the refresh string against the existing content control string and skip the refresh if its unchanged. What you're currently doing seems to imply reverting to the original content even when the refresh changes it. In that case, you may as well have locked the content control to prevent updating...

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, November 1, 2015 4:53 AM
  • Hi Luke415,

    What’s the result of using Range.WordOpenXML and Range.InsertXML to remain text and formatting?

    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 2, 2015 2:00 AM
    Moderator