Getting formatted text from Range to RichTextBox and return

  • Thread starter Jens Gyldenkærne Clausen
  • Start date
J

Jens Gyldenkærne Clausen

Hello again. Is there a way to get formatted text from a Word document
(specifically a Range within the active document) to a
RichTextContentControl in a Form (and/or backwards).

I can easily get the plain text from the Range to the RichTextControl
and back, but I would like to include the text formats - highlighting,
bold/italic, and if possible embedded controls and images.

It doesn't have to be a RichTextControl on the form - if there's an
easier way to show a small part of the document content in a way that
facilitates editing, that'll work fine too. But right now I'm stuck
because I can't find a way to populate the RichTextBox with the
formatted range text.

Thanks for any help.
 
J

Jay Freedman

Jens said:
Hello again. Is there a way to get formatted text from a Word document
(specifically a Range within the active document) to a
RichTextContentControl in a Form (and/or backwards).

I can easily get the plain text from the Range to the RichTextControl
and back, but I would like to include the text formats - highlighting,
bold/italic, and if possible embedded controls and images.

It doesn't have to be a RichTextControl on the form - if there's an
easier way to show a small part of the document content in a way that
facilitates editing, that'll work fine too. But right now I'm stuck
because I can't find a way to populate the RichTextBox with the
formatted range text.

Thanks for any help.

This snippet assumes that the formatted text to be copied is the first
paragraph of the document, and that the RichText content control is the
first (or only) content control in the document.

Sub demo()
Dim rgSrc As Range, rgDest As Range
Set rgSrc = ActiveDocument.Paragraphs(1).Range
rgSrc.MoveEnd wdCharacter, -1
Set rgDest = ActiveDocument.ContentControls(1).Range
rgDest.FormattedText = rgSrc.FormattedText
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top