none
Copy RichTextContentControl text with formatting RRS feed

  • Question

  • I can't figure out how to copy the text of a RichTextContentControl. If I use:

    mycontrol.Range.Copy()

    And then paste (ctrl+v) somewhere else in the document, the content control itself is also copied. This is not what I want, I only want the formatted text. Is there any way to do this?

    cheers,
    Otto


    • Edited by 0tto Friday, August 17, 2012 11:12 AM Clarify title
    Friday, August 17, 2012 8:00 AM

Answers

  • Hi Otto

    The problem you're having is that you're including the content control in the Range you're copying. You'd first need to move the Range "inside" the content control. Here's some sample VB code that has worked for me:

        Dim docTarget As word.Document = wdApp.ActiveDocument
        Dim ccSource As word.ContentControl = Documents("Document1").Contentcontrols(2)
        Dim rngSource As word.Range = ccSource.Range
     
        'rngSource.MoveStart(Word.WdUnit.wdCharacter, 1)
        rngSource.MoveEnd(Word.WdUnit.wdCharacter, -1)
        docTarget.Content.FormattedText = rngSource.FormattedText

    This sample uses two documents, the currently active one and a document automatically generated by Word named "Document1". You don't need two documents, this is just the sample code I have lying around.

    Important for you is the way the Range for the source content control is manipulated. You'll see that I shorten it by one character at each end.

    Please also note that this sample code uses the FormattedText property, rather than Copy, to transfer the Range. This does not use the Windows Clipboard, which is usually a good idea unless you specifically have a reason to do so. 

    Edit: I noticed after actually clicking on the test result, at a later time, that the original code I copied actually makes sure the content control IS copied. So I've changed it to make sure it copies only the content. Note that it is not necessary to move the Range.Start, only the Range.End - back it up by one character.


    Cindy Meister, VSTO/Word MVP


    • Proposed as answer by JosephFox Sunday, August 19, 2012 9:13 AM
    • Edited by Cindy Meister MVPModerator Sunday, August 19, 2012 10:05 AM corrected code
    • Marked as answer by 0tto Wednesday, August 22, 2012 6:54 AM
    Sunday, August 19, 2012 9:09 AM
    Moderator

All replies

  • I think you'd need to use a general C# class, probably the Clipboard Class. Try the SetText method with myControl.Text.
    Friday, August 17, 2012 11:05 AM
  • But then I will loose the formatting of the text. As far as I know the myControl.Text property is text only.
    Friday, August 17, 2012 11:12 AM
  • I remember a thread, which unfortunately I haven't been able to find, in which someone used the XMLMapping property of a content control to do this.

    But anyway, seeing an unrelated thread made me think, have you tried myControl.Range.FormattedText.Copy()?

    Friday, August 17, 2012 10:14 PM
  • Hi Otto

    The problem you're having is that you're including the content control in the Range you're copying. You'd first need to move the Range "inside" the content control. Here's some sample VB code that has worked for me:

        Dim docTarget As word.Document = wdApp.ActiveDocument
        Dim ccSource As word.ContentControl = Documents("Document1").Contentcontrols(2)
        Dim rngSource As word.Range = ccSource.Range
     
        'rngSource.MoveStart(Word.WdUnit.wdCharacter, 1)
        rngSource.MoveEnd(Word.WdUnit.wdCharacter, -1)
        docTarget.Content.FormattedText = rngSource.FormattedText

    This sample uses two documents, the currently active one and a document automatically generated by Word named "Document1". You don't need two documents, this is just the sample code I have lying around.

    Important for you is the way the Range for the source content control is manipulated. You'll see that I shorten it by one character at each end.

    Please also note that this sample code uses the FormattedText property, rather than Copy, to transfer the Range. This does not use the Windows Clipboard, which is usually a good idea unless you specifically have a reason to do so. 

    Edit: I noticed after actually clicking on the test result, at a later time, that the original code I copied actually makes sure the content control IS copied. So I've changed it to make sure it copies only the content. Note that it is not necessary to move the Range.Start, only the Range.End - back it up by one character.


    Cindy Meister, VSTO/Word MVP


    • Proposed as answer by JosephFox Sunday, August 19, 2012 9:13 AM
    • Edited by Cindy Meister MVPModerator Sunday, August 19, 2012 10:05 AM corrected code
    • Marked as answer by 0tto Wednesday, August 22, 2012 6:54 AM
    Sunday, August 19, 2012 9:09 AM
    Moderator
  • Sorry for the late reply. This worked great Cindy, thank you very much.
    Wednesday, August 22, 2012 6:55 AM