kewpcg

I am upgrading my VB code to a VSTO Add-in for Word.

In Visual Basic 6.0, I was able to copy some RTF from my Word document to the clipboard, then use the Richtextbox.TextRFT method to insert it in the form.

The .Net version of the RichTextBox does not have a TextRTF method. I searched around a bit, and it seems I may need to create a FlowDocument object to handle this step. However, I can't seem to get a reference to this library (System.Windows.Documents) from my VSTO Add-in.

Can someone help me get started with this How can I place a RTF string into a VB.Net Richtextbox

Thanks

Karen



Re: Visual Studio Tools for Office How to get RTF text into a RichTextbox in VS.NET?

Helmut Obertanner

Hello Karen,

since I'm a C# coder I can only point you this sample.

I tried and it seems to work.

I hope you can translate it to VB

if (Clipboard.ContainsText(TextDataFormat.Rtf))

{

string text = Clipboard.GetText(TextDataFormat.Rtf);

MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(text));

richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText);

stream.Close();

}

You need at last a reference to the System.Windows.Forms dll in your project.

I hope this helps,

greets, Helmut






Re: Visual Studio Tools for Office How to get RTF text into a RichTextbox in VS.NET?

kewpcg

This works nicely, thank you.

If it is not too much to ask... what if we want to accomplish the same in reverse In other words, get the RTF text out of the RichTextbox, and place it in the active Word document

I noticed I can do richTextBox1.Copy, followed by ThisAddin.Application.Selection.Paste but I have to select everything in the RichTextbox first (or richTextBox1.SelectAll), which I would like to avoid.

Is there a more elegant way to get the RTF from the control into the Word document

Thanks

kewpcg





Re: Visual Studio Tools for Office How to get RTF text into a RichTextbox in VS.NET?

Helmut Obertanner

Hello,

sometimes things are easier:

How about that

if (Clipboard.ContainsText(TextDataFormat.Rtf))

{

richTextBox1.Rtf = Clipboard.GetText(TextDataFormat.Rtf);

}

Clipboard.SetText(richTextBox1.Rtf, TextDataFormat.Rtf);

this.Application.Selection.Paste();

Hope this helps,

greets, Helmut






Re: Visual Studio Tools for Office How to get RTF text into a RichTextbox in VS.NET?

kewpcg

Wow, I didn't see the .RTF propery on the richtextbox. That helps a lot. Thanks!