Word: How to do “Drag and Drop” of Content controls from Task Pane to the document
Create a Word VSTO application level project for Word using Visual Studio 2008/2005 (For VS 2005, you need to install VSTO SE). To do that, go to File Menu in VS -->New-->Project-->Expand Office hive-->Select Word 2007 Add-in
Add a user control item (Name it as CTP.cs) to the project and insert a List box (Name it as, lstItems). To add a user control to the project, go to Project Menu-->Add User Control-->Name it as CTP.cs
Add the code snippet below in the ThisAddIn_Startup event (You will see this when you view the code for ThisAddin.cs). This will add our custom task pane while loading Word
Microsoft.Office.Tools.CustomTaskPane WCTP;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
//To add our CTP to the Custom Task panes collection of Word. We need to set the Visible property to true, otherwise, by
//default it is false
WCTP = (Microsoft.Office.Tools.CustomTaskPane)this.CustomTaskPanes.Add(new CTP(), "Do Drag Drop");
WCTP.Visible = true;
}
The code snippet that we need to have in the user control class is as below,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
namespace DragDrop_ContentControls
{
public partial class CTP : UserControl
{
public CTP()
{
InitializeComponent();
}
object missing = System.Type.Missing;
private Rectangle dragBoxFromMouseDown;
private void lstItems_MouseDown(object sender, MouseEventArgs e)
{
// Remember the point where the mouse down occurred. The DragSize indicates
// the size that the mouse can move before a drag event should be started.
Size dragSize = SystemInformation.DragSize;
// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
e.Y - (dragSize.Height / 2)), dragSize);
}
private void lstItems_MouseMove(object sender, MouseEventArgs e)
{
//To check if the Mouse left button is clicked
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
//Start Drag Drop
DoDragDrop("", DragDropEffects.All);
Word.ContentControl cntCtrl;
//Insert Content Control at the selected range
cntCtrl = Globals.ThisAddIn.Application.Selection.Range.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText, ref missing);
cntCtrl.Range.Text = lstItems.Text; //This is how we set the value to be to populated in the content controls
//To set the XPath for custom XML binding for content controls. This is optional
string xPath = @"Root\" + lstItems.Text.Replace(" ", "_");
cntCtrl.XMLMapping.SetMapping(xPath, "", null); //This is required when you want to bind to a custom XML part
}
}
}
private void lstItems_MouseUp(object sender, MouseEventArgs e)
{
// Reset the drag rectangle when the mouse button is raised.
dragBoxFromMouseDown = Rectangle.Empty;
}
}
}
Build the application and Run it to launch Word
When you drag an item from List box in the Task Pane onto the document surface, you will see a content control being added with the caption of the list item name
Comments
- Anonymous
January 31, 2011
Doesn't work. No error but the drag-and-drop effect won't work. :(