Freigeben über


How the Actions Pane Interacts with Word Documents

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:

Then I added the following code to the UserControl1 class:

public UserControl1()
{
InitializeComponent();
this.button1.Click +=new EventHandler(button1_Click);
}

void button1_Click(object sender, EventArgs e)
{
Globals.ThisDocument.Bookmark1.Text =
this.monthCalendar1.SelectionStart.ToLongDateString();
}

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
{
UserControl1 uc1;

private void ThisDocument_Initialize(object sender, System.EventArgs e)
{
uc1 = new UserControl1();
this.ActionsPane.Controls.Add(uc1);
this.Bookmark1.Selected +=
new Microsoft.Office.Tools.Word.SelectionEventHandler
(Bookmark1_Selected);
}

...

void Bookmark1_Selected(object sender,
Microsoft.Office.Tools.Word.SelectionEventArgs e)
{
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:

I select a range of dates, such as May 4th through May 6th, and then l click button1. The long date version of the first selected date in the date range (Tuesday, May 04, 2004) is added to the Bookmark view control. Similarly, when I select something inside of the Bookmark view control, the view control's text is added to the UserControl1's Label control.

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 https://www.microsoft.com/info/cpyright.htm.

Comments

  • Anonymous
    April 29, 2004
    Great post Paul. Thanks. I get more excited about this stuff with each post.

    Chris
  • Anonymous
    May 07, 2004
    Hi guys,

    I'm really confused about getting a reference to 'this.ActionsPane.Controls.Add'

    This article mentions, "...We expose a Globals class that allows you access to ThisDocument.." How exaclty do I do that?

    Any ideas, or source code? hejviking@hotmail.com

    thanks!

    Ed
  • Anonymous
    May 07, 2004
    First of all, are you using Microsoft Visual Studio Tools for the Microsoft Office System, Version 2005 Beta? The Globals class, as well as the actions pane, doesn't exist in solutions created with Visual Studio Tools for Office, Version 2003.
  • Anonymous
    May 07, 2004
    Oh (@&#&@..

    So, how do I get 'Version 2005 Beta'?

    Thank you verrrryy much.

    Ed
  • Anonymous
    May 07, 2004
    It is currently part of of the Microsoft Visual Studio 2005 Community Technology Previews. These were handed out at events such as VSLive! earlier this year and will be handed out at additional events to be announced. If you are an MSDN Universal subscriber, you should also be getting access to these through your subscription CDs and the MSDN Universal Web download site.