Using the Drag-and-Drop Operation with VSTO Add-ins in Word 2010

Office 2010

Summary:  The drag-and-drop operation is one of the most common operations in a smart client application. Windows forms and Windows Presentation Foundation (WPF) are great technologies that enable developers to intercept drag-and-drop operations in Microsoft Word 2010.

The drag-and-drop operation is one of the most common in a smart client application. Windows forms and WPF enable developers to intercept the drag-and-drop operation. Achieving the same result in Microsoft Word 2010 documents by using managed add-ins can be complex.

Imagine that you have a Microsoft Office development tools in Microsoft Visual Studio 2010 add-in that displays a list of orders or some other content in a custom task pane, and that the user is trying to follow these steps:

  • Select text that represents an order in the list box

  • Drag the order from the list box onto the Word 2010 document window

  • Drop the order on the Word 2010 document window at a specific location

  • Present the details of the dropped order in the form of a table in Word 2010

The fourth task, presenting the details, can be complex, because Word 2010 has its own way to identify the drag-and-drop operation, and it inserts the dropped object as-is. The following sections explain how to complete this task as described in the final step of the previous list. They explain a simple way of intercepting the drop operation on a Word document and performing custom processing on the dropped object, by using a Office development tools in Visual Studio 2010 add-in.

To determine the drop operation in a Word document, you can listen for the WindowSelectionChange event. Unfortunately, this event is also raised any time that the selection changes in the document.

The following steps explain how you can avoid this limitation.

First, build a managed add-in project in C# that adds text to a list box in a custom task pane.

When the user selects an item in the list box and drags that item into the Word document, the code performs the following operations:

  • It starts the drag-and-drop operation when the user selects the text to drag in the list box in the custom task pane.

  • When the user drags the text out of the list box control, the add-in draws a transparent Windows form on the active Word document.

    This form can listen for drop events (much like a regular Windows form can).

  • When the drop occurs on the Windows form, the form coordinates are passed to the Word document, and the add-in then inserts the content by using the Word object model.

The add-in draws a transparent form that intercepts drop events on the Word document. This gives the impression that the drop happens in Word, although it is actually occurring on the Windows form. When the user starts the drop, the add-in takes the coordinates and sends that data to the Word document with a method call.

To position the Windows form on the Word window, you must have a handle to the Word window. The next section explains how to obtain this handle.

Figure 1 shows the flow of the process.

Figure 1. Flow of events during the drag-and-drop operation

Flow of events during the drag-and-drop

Identifying the Active Word Window Handle and Positioning the Form

The following procedure shows how to determine the active Word window handle and position the form.

To determine the active Word window handle and position the form

  1. Identify the Word window where the form will be positioned.

  2. Use the native Windows function FindWindowsEx to get the handle.

  3. To position the form, get the rectangular coordinates of the Word window.

  4. Set this form as a parent to the Word window

Note Note

Setting this form as a parent window has the advantage that the form closes when the Word window is closed.

The following code example shows this operation.

private void OverlayForm()
        {
            if (form == null)
            {
                form = new OverlayForm();
                form.AllowDrop = true;
                IntPtr parent = GetCurrentWindowHandle();
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
                WinAPI.GetWindowRect(parent, ref rect);
                uint uFlags = WinAPI.SWP_NOACTIVATE | WinAPI.SWP_NOZORDER;
                WinAPI.SetWindowPos((uint)form.Handle.ToInt32(), parent.ToInt32(), 0, 0, rect.Right, rect.Bottom, uFlags);
                WinAPI.SetParent(form.Handle, parent);
                form.AllowTransparency = true;

                Globals.ThisAddIn.OverlayForm = form;
                form.Show();
            }
            else
            {
                if (form.Visible == false)
                {
                    form.Show();
                }

            }
        }

For more information about the native Windows methods that are used in the code, see the following topics:

Listening for Drop Events in a Windows Form

The Windows Form must contain a control that listens for drop events. This control can be a text box that spans the form. For more information, see Performing Drag-and-Drop Operations in Windows Forms.

Passing the Coordinates to Word

When the drop operation occurs, the DragDrop event handler on the form control is invoked. The coordinates of the form control are available from the event arguments. To obtain the Word coordinates, use the RangeFromPoint method of the ActiveWindow object, as shown in the following code example.

Microsoft.Office.Interop.Word.Range range = (Microsoft.Office.Interop.Word.Range)Globals.ThisAddIn.Application.ActiveWindow.RangeFromPoint(e.X, e.Y);

The code sample that accompanies this article describes how to use the drag-and-drop operation in a Word document in Office development tools in Visual Studio 2010 add-ins.

The sample creates a list box control that contains a list of orders and enables the user to drag these orders into the Word document window. When the user performs the drag-and-drop operation from the list box control a Windows form is drawn, as previously mentioned. Once the drop occurs on the Windows form, the add-in invokes the OnDropOccurred method, passing the Word coordinates of the drop location. The add-in then creates a Word table at those coordinates, and the values of the table cells is read from the data that is being passed as a parameter, as shown in Figure 2.

Figure 2. Drag-and-drop list items

Drag-and-drop list items

To see the actual form that is drawn, do the following:

  1. Set the opacity of the overlay form to 100%

  2. Set the foreground color and background color of the text box control to Gray.

Hiding the Windows Form

Once the drop is complete, the Windows form should be hidden so that it does not actually appear on the document. The code hides the form when the WindowActivate event is raised.

To see why this is necessary, consider the following scenario:

  • A user drags text out of the custom task pane’s list-box control.

  • By pressing Alt+Tab, the user drops the text on another location, outside the Word document.

Under these circumstances, the form still appears on the Word document

To avoid this, the add-in hides the form when the user re-starts the Word window, thereby raising the WindowActivate event.

Running the Code Samples

The following are the prerequisites required to run the code samples:

  • Microsoft .NET Framework 4 (download).

  • Microsoft Visual Studio 2010 Tools for Office Runtime (download).

To run the code sample

  1. Build the sample project and then press F5.

    A Word document opens, displaying a custom task pane that contains a list box control.

  2. Select an item from the list box.

  3. Drag the item onto the Word document window.

    A table appears in the Word document window at the drop location. Each cell of the table contains the text that represents the selected order.

This article presents a simple method for capturing drag-and-drop operations in Word when there is a need for custom processing before an object is dropped on the Word document window.

Anil Kumar is a consultant with Microsoft Global Services India. He specializes in developing Office solutions using Open XML and VSTO on the .NET Framework.You can reach him at ankumar@microsoft.com.

Show:
© 2014 Microsoft