mathmax

Hello,

I've a control in my word 2007 document and I would like to get the Range that contains it. If the control already exist on my page, I know I can add a bookmark around it and then get the Range via the Range property. But if the control is added at runtime, I don't find a way to add a bookmark around it...

As a general rule, is there an other way to find the Range that contains a control Or must I inevitably add a bookmark around the control

Thank you in advance for your help.



Re: Visual Studio Tools for Office How to get the Range that contains a control.

McLean Schofield - MSFT

Assuming that you are using a Word Document or Word Template project in Visual Studio 2008, there are several ways to do this, depending on the type of control you're referring to.

If you are using a "host control" for Word (that is, a VSTO-supplied control that adds features to a "native" Word object, such as the ComboBoxContentControl), each of these objects have a Range property. If you are using one of the Windows Forms controls in VSTO, each of these objects have an InlineShape property that exposes the Range.

The following code example demonstrates both options.

this.Paragraphs[1].Range.InsertParagraphBefore();
this.Paragraphs[1].Range.InsertParagraphBefore();

// Demonstrate content control range.
Microsoft.Office.Tools.Word.ComboBoxContentControl comboBox1 =
this.Controls.AddComboBoxContentControl(this.Paragraphs[1].Range,
"comboBox1");
MessageBox.Show(String.Format("The content control range starts at {0} and ends at {1}.",
comboBox1.Range.Start,
comboBox1.Range.End));

// Demonstrate Windows Forms control range.
Microsoft.Office.Tools.Word.Controls.Button button1 =
this.Controls.AddButton(this.Paragraphs[2].Range, 50, 20, "button1");
MessageBox.Show(String.Format("The button range starts at {0} and ends at {1}.",
button1.InlineShape.Range.Start,
button1.InlineShape.Range.End));

I hope this helps,

McLean Schofield






Re: Visual Studio Tools for Office How to get the Range that contains a control.

mathmax

My control is a user control. It has neither InlineShape property neither Range property.





Re: Visual Studio Tools for Office How to get the Range that contains a control.

McLean Schofield - MSFT

You can apply the same concept to System.Windows.Forms.UserControl objects that you add to the document surface programmatically at run time. The only difference is that to add a UserControl, you use the general AddControl helper method, and the returned object is a Microsoft.Office.Tools.Word.OLEControl. For more information, see the heading "Adding Custom Controls" in http://msdn2.microsoft.com/en-us/library/x97a5x3s(VS.90).aspx.

The following code example assumes that you have a UserControl that is named UserControl1 in your project.

this.Paragraphs[1].Range.InsertParagraphBefore();
UserControl1 userControl1 = new UserControl1();

// Demonstrate Windows Forms user control.
Microsoft.Office.Tools.Word.OLEControl oleControl1 =
this.Controls.AddControl(userControl1, this.Paragraphs[1].Range,
200, 100, "userControl1");

MessageBox.Show(String.Format("The user control range starts at {0} and ends at {1}.",
oleControl1.InlineShape.Range.Start,
oleControl1.InlineShape.Range.End));






Re: Visual Studio Tools for Office How to get the Range that contains a control.

mathmax

Ok, and what if my user control already exist. Should I cast it to a OLEControl





Re: Visual Studio Tools for Office How to get the Range that contains a control.

mathmax

How to get the Range that contains a usercontrol that was dragged and dropped at design time