In an earlier post, I said that I would show you the code that allows a Microsoft Office document and an actions pane's UserControls to interact with each other. Here's a very simple example of how to do this.
First, I created a C# Word Application project and added a Bookmark view control to the Word document. Dragging view controls onto documents is very easy using the Toolbox, as shown here:
Next, I added a UserControl to the project. I added a MonthCalendar control (monthCalendar1), a Button control (button1), and a Label control (label1) to the UserControl so that it looks like this:
this.button1.Click +=new EventHandler(button1_Click);
void button1_Click(object sender, EventArgs e)
This code takes the beginning date of an array of one or more dates that I select in the MonthCalendar control and inserts it into the Bookmark view control on the Word document. The date is formatted as a long date (for example, Tuesday, May 04, 2004).
How does the UserControl know about the existence of the document? We expose a Globals class that allows you access to ThisDocument (and, by default, the Sheet1, Sheet2, Sheet3, and ThisWorkbook for Microsoft Office Excel based projects). When you type a dot after Globals, you get an IntelliSense list with access to the Office document's related classes.
Next, I added the following code to the ThisDocument class:
public partial class ThisDocument
private void ThisDocument_Initialize(object sender, System.EventArgs e)
uc1 = new UserControl1();
void Bookmark1_Selected(object sender,
this.uc1.label1.Text = this.Bookmark1.Text;
After I declare and instantiate an instance of the UserControl, I can access it through code. The Controls.Add method, as its name implies, adds the UserControl to the actions pane and displays the actions pane. One thing to note is that in order to access an individual control on the UserControl, you need to set the individual control's Modifiers property to Internal. Otherwise, it's not available to your document's code and of course you wouldn't see it in the IntelliSense list.
When I run the document, I get an experience like this:
In a future post, we'll create multiple UserControls and show you to display different UserControls in the actions pane based on wherever the user's cursor is in the document. We'll also show you how to change the UserControls' layout based on whether the actions pane orientation is on the right edge or top edge of the document.
-- Paul Cornell
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm.