Reading Rich Text into Microsoft Office 2007 using VSTO

Posted on 2009-04-29
Last Modified: 2013-11-10
I am developing C#.Net application in which Rich Text is created in one of several several Rich Text Edit boxes in the application. I am also using Visual Studio Tools for Office in order to Programmatically add the Rich Text content into a Microsoft Office document in various locations.  My problem is that I have not been able to get the Rich Text read in preserving the formatting.  I have tried using a ContentControl and setting the Type = WdContentControlType.wdContentControlRichText, but the Word Document only interprets it as raw text.  Should I be using some other technique?

I'm really not even sure how to read text into the ContentControl.  I have tried using a Range, but maybe that's not the correct way.

I am using C#.Net with Visual Studio 2008 and Microsoft.Office.Core and Microsoft.Office.Interop.Word.
Question by:thomehm
    LVL 23

    Accepted Solution

    You could take a look at the following MS KB - which describes how to create an RTF string, copy the RTF string to the clipboard, and then paste the RTF string into the document.  You could convert the VB code that is there into C# and modify it so that the text is copied into the content control rather than the Word document itself.

    Author Comment

    I am using the Clipboard to insert the Rich Text, my code is a little simpler and different:

                        Clipboard.SetData(DataFormats.Rtf, strDescription);
                        RowCurrent.Cells[defs.ICIName + 1].Range.Paste();

    It seems to work for me.
    LVL 23

    Expert Comment

    Glad to hear that you came up with something that worked...thanks for posting the code (I agree, your code is much simpler)!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
    Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
    This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now