ClaudiaHelpOnVSTO

Hi,
I am creating a .doc with VSTO2005. My document contains several Controls such as Buttons, RichTextBoxes, ComboBoxes, etc. which are generated at runtime.

In some cases, I encounter the problem that a RichTextBox doesn't display the content (text) it should, but only displays a big red cross like this:
\   /
  \
/   \

When I click into the RichTextBox field (with the red cross in it), the following, weird message appears:

¡°Windows Forms controls have been disabled because the document has been scaled. When zoom is returned to 100%, controls will reactivate.¡±

But actually the zoom of the document hasn¡¯t changed at all!

Has anyone already encountered this problem

Thanks for any hint, Claudia


Re: Visual Studio Tools for Office Strange behavior: RichTextBox

Cindy Meister

Hi Claudia

What is the Zoom of the document (View/Zoom)

Windows Forms controls really work only when the zoom is 100%.






Re: Visual Studio Tools for Office Strange behavior: RichTextBox

Geoff Darst - MSFT

Hi Claudia,

Using RichTextBox controls in VSTO isn't supported. The problem is that the Windows Forms Rich TextBox control was designed to wrap the Win32 Rich Edit Control. However, Office has its own version of that control which behaves slightly differently and will always be loaded. Since the Windows Forms RichTextBox wasn't designed to wrap this version of the RichEdit control we can't guarantee how it will behave.

Sincerely,

Geoff Darst

Microsoft VSTO Team





Re: Visual Studio Tools for Office Strange behavior: RichTextBox

ClaudiaHelpOnVSTO

Hi,

Thanks for your reply. I was busy resolving other problems... but know I am back on the VSTO project! I was quite surprised to read your response... so far I have never come across the information that RichTextBox controls were not supported in VSTO. (Especially the doc page on controls - ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vstcore/html/4f43b3eb-f0ec-44e2-9885-6ede327c6913.htm
- do not provide any information on this!)

Would a declaration of a RichtTextBox control like this always (!) work in Word
System.Windows.Forms.RichTextBox richTextBox1 = new RichTextBox();

Microsoft.Office.Tools.Word.OLEControl dynamicControl =
this.Controls.AddControl(richTextBox1 , range1, 56.25F, 17.25F, "dynamic");

Thanks,
Claudia




Re: Visual Studio Tools for Office Strange behavior: RichTextBox

ClaudiaHelpOnVSTO

Hi,
I tried to work out the previous idea of using the AddControl() method... but this didn't work out.

As I need the control to behave correctly in every situation (!), could you give my an advice either how I can access and integrate the Win32 RichEdit control of Office
or
how to wrap the Windows.Forms.RichTextBox correctly to avoid the malfunction of the control

Thanks a lot!

Claudia




Re: Visual Studio Tools for Office Strange behavior: RichTextBox

ClaudiaHelpOnVSTO

Hi,
I think my question got lost... so lets repeat it :-)

How can I access and integrate the Win32 RichEdit control of Office
or
how can I wrap the Windows.Forms.RichTextBox correctly to avoid the malfunction of the RichTextBox control

Thanks,
Claudia




Re: Visual Studio Tools for Office Strange behavior: RichTextBox

Geoff Darst - MSFT

Wierd, I could have sworn I replied to this yesterday.  Maybe I got started and then never submitted...Anyway, this is a bit of a tough problem.  Office will always load their own version of the Rich Edit control at runtime.  It will be there prior to VSTO being loaded so there is no way for your VSTO customization to sneak in and load the right control.  Even if you could do that, you would presumably be breaking Office functionality.

Probably the simplest solution would be to not use a managed control at all.  Office still ships the RichTextbox ActiveX control (RichTxt32.Ocx).  You could use the Office object model to add it programatically.  To talk to it, you could either use reflection or you could generate your own interop assembly to reference using TlbImp.exe.  The latter would make for easier coding, but you would need to deploy the assembly with your customization.

If you didn't want to do that, the other approach would be to write your own managed version of the RichTextbox control which wrapped the Office version of RichEd20.dll.  Essentially, you would derive from System.Windows.Forms.Control and delegate the appropriate overrides to the Win32 control using standard windows messages.  You would need to ensure you locate the correct version of the control by calling LoadLibrary with an explict path.  This approach would work, but it would be pretty labor intensive and would require good P/Invoke skills and a knowlege of Win32 controls programming.

Yes, I'm sorry that the control is documented.  It probably shouldn't have been.  While some folks have been able to make it work, we aren't in a position to support it because of the fact that it wraps the wrong version of the control when used from Office.

Sincerely,

Geoff Darst

Microsoft VSTO Team





Re: Visual Studio Tools for Office Strange behavior: RichTextBox

ClaudiaHelpOnVSTO

Hi Geoff,
Thanks for your reply. Reading your answer made me realize that I am completely lost as soon as I am asked to code something without using the well documented .NET or VSTO API :-((

I tried to find the RichTextBox control in the Office object model... in vain.
How can I access the RichTextBox ActiveX control (RichTxt32.Ocx) using reflection A little code example would be very much appreciated :-)

Thanks for your help,
Claudia





Re: Visual Studio Tools for Office Strange behavior: RichTextBox

Cindy Meister

Hi Claudia

Office doesn't have a RichTextbox control. The one you mention is distributed with classic VB6, as far as I know...






Re: Visual Studio Tools for Office Strange behavior: RichTextBox

Geoff Darst - MSFT

Well I'm embarassed. We used to ship this control in Office (as well as VB 6.0) -- see http://support.microsoft.com/kb/297391. Since I had this control on my machine, I incorrectly assumed we were still shipping it with Office. What I'd forgotten was that I had to put VB6 on the other day to investigate a legacy test issue. Anyway, it appears that Cindy may be right that we quit shipping this control in Office.

That doesn't leave you with much in the way of options. The problem with this control is that it does seem to work in some scenarios. We pulled it late in the product cycle from the designer when it became clear that we couldn't get it to work well enough, but IIRC we may have deliberately left the runtime piece in because we had Beta customers who were already using the control in specific scenarios. What we should have done is pulled the documentation.

Can you describe your scenario as far as when you are creating the controls The red-X is a drawing exception; the only thing I can think of is that maybe there is something unusual that you are doing around your control creation that is affecting the behavior. Perhaps your creation code can be tweaked somehow to get you around this problem. As I mentioned, we can't really support this control, but if your scenario is limited enough, you may be able to use it anyway.

Sincerely,

Geoff Darst

Microsoft VSTO Team





Re: Visual Studio Tools for Office Strange behavior: RichTextBox

ClaudiaHelpOnVSTO

Hi,
Thanks for both replies! I don't think I do 'unusual' things with the RichTextBox control :-)
But what I do have in my word document is a lot of different controls... and what I have noticed is, that as soon as I have more than one RichTextBox control in the same document, the first RichTextBox displays correctly where as the others (usually - but not always) fail to display the content. And especially if there is a ComboBox or a DateTimePicker in between two RichTextBox controls, the second RichTextBox always "crashes" (displaying the red-X).

Here a code snipped when inserting the RichTextBox control:
using Controls = Microsoft.Office.Tools.Word.Controls;
...
// add rich textbox
Controls.RichTextBox box = Globals.ThisDocument.Controls.AddRichTextBox(
table.Cell(2, 1).Range, width, height, fieldIdentifier);
box.BorderStyle = BorderStyle.None;
// content
box.Name = fieldIdentifier;
box.Paste();

Claudia





Re: Visual Studio Tools for Office Strange behavior: RichTextBox

Geoff Darst - MSFT

Hi Claudia,

"Unusual" is always relative :). Well, what I would suggest would be to try some subtle (or not so subtle) tweaks to your code. The first thing I would try would be to see if changing the size of your RichTextBoxes makes a difference. Also, I'd try the same code, but outside the Word table. You might also try creating the RichTextBox controls as Shapes instead of in-line (use the other override fro AddRichTextBox). Finally, I'd try removing the paste to see if that makes a difference. Hopefully, one of these approaches will both work and be workable...

Sincerely,

Geoff Darst

Microsoft VSTO Team





Re: Visual Studio Tools for Office Strange behavior: RichTextBox

ClaudiaHelpOnVSTO

Hi Geoff,
I have been trying around and ...
- removing the past() methode didn't work
- there is no addRichTextBox() methode in the Shape object as far as I could see [there is only a addTextBox()]
- ...

So I finally replaced the RichtTextBox control by the simple TextBox control. Now I have no red cross or anything bizarre, but unfortunaltely the TextBoxs don't take up the predefined size... they show up as a tiny textBox with a height equal to a line.

So know my question is: how can I make sure that Word allocates enough place (range space) for each textBox

I don't know if my explanations are clear enough... if you try and run this simple code below you will normally see a first a little textbox followed by a big textbox. But in the code I define both textboxs in the same way! How can I fix this problem
This problem could also be the source of the richtextbox draw-exceptions... !

----
using Controls = Microsoft.Office.Tools.Word.Controls;

public partial class ThisDocument
{
object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;

private void ThisDocument_Startup(object sender, System.EventArgs e)
{
Word.Range range = Range(ref missing, ref missing);
object start = missing;

// 1 textbox
Controls.TextBox box = this.Controls.AddTextBox(
range, 300, 200, "textbox_1");
box.BorderStyle = BorderStyle.None;
box.Multiline = true;
box.ScrollBars = ScrollBars.Vertical;
box.WordWrap = true;
box.Text = @"
Word.Range range = this.Range(ref missing, ref missing);
Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, vScrollBar);

Microsoft.Office.Tools.Word.Controls.TextBox text = this.Controls.AddTextBox(range, 15, 50, textBox);
text.Multiline = true;
Word.Range range = this.Range(ref missing, ref missing);
Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, vScrollBar);

Microsoft.Office.Tools.Word.Controls.TextBox text = this.Controls.AddTextBox(range, 15, 50, textBox);
text.Multiline = true;
Word.Range range = this.Range(ref missing, ref missing);
Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, vScrollBar);

Microsoft.Office.Tools.Word.Controls.TextBox text = this.Controls.AddTextBo";

range.InsertParagraphAfter();
range.InsertParagraphAfter();
range.Collapse(ref collapseEnd);


// 2 textbox
Controls.TextBox box2 = this.Controls.AddTextBox(
range, 300, 200, "textbox_2");
box2.BorderStyle = BorderStyle.None;
box2.Multiline = true;
box2.ScrollBars = ScrollBars.Vertical;
box2.WordWrap = true;

box2.Text = @"
Word.Range range = this.Range(ref missing, ref missing);
Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, vScrollBar);

Microsoft.Office.Tools.Word.Controls.TextBox text = this.Controls.AddTextBox(range, 15, 50, textBox);
text.Multiline = true;
Word.Range range = this.Range(ref missing, ref missing);
Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, vScrollBar);

Microsoft.Office.Tools.Word.Controls.TextBox text = this.Controls.AddTextBox(range, 15, 50, textBox);
text.Multiline = true;
Word.Range range = this.Range(ref missing, ref missing);
Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, vScrollBar);

Microsoft.Office.Tools.Word.Controls.TextBox text = this.Controls.AddTextBo";

range.InsertParagraphAfter();
range.InsertParagraphAfter();
range.Collapse(ref collapseEnd);

}
...
}

Thanks a lot,
Claudia








Re: Visual Studio Tools for Office Strange behavior: RichTextBox

Geoff Darst - MSFT

Hi Claudia,

Hmmm. There is definitely something strange going on here. Word does some deferred processing with respect to controls and it looks like that is factoring in here. When you call AddTextBox, the control has been created, but it looks like Word hasn't actually sized it yet. If you check the height of the control after the call, it is still at 15. What is even more interesting is that if you add a MessageBox.Show call after adding the first control, you will get the expected behavior (that is both controls will be sized the same). Presumably, the modal dialog allows some messages to get processed that are necessary for the control to get resized. It also appears that you can work around this by "resizing" the first control after you insert the second. If I add the following code after the AddTextBox call for box2, I get the correct behavior:

box.Width = 300;

box.Height = 200;

Does this help at all Would you be able to use this as a workaround to enable your scenario

Sincerely,

Geoff Darst

Microsoft VSTO Team





Re: Visual Studio Tools for Office Strange behavior: RichTextBox

ClaudiaHelpOnVSTO

Hi Geoff,
I'll try this out.. and let you know.

Thanks,
Claudia