Etudes for Microsoft Word Programmers
Etude 3.2. Customizing Drag-n-Drop for Microsoft Word Document Window
"Drag and Drop", abb. DnD, is the name of a technique of working with objects in window interface. The user can drag selected text, selected files or other objects from one window to another as well as between the windows of different applications. It is quite clear that the applications should support this technique, otherwise, dragging wonít work.
The simple implementation of Drag-n-Drop customization for Microsoft Word document window is studied in this etude. The etude implementation consists of the "AhCustomDragDrop.dot" (VBA) template and the "AhCustomDragDrop.dll" (C++) library. The source code of the library is not included.
The necessity of Drag-n-Drop customization appears while using Microsoft Word as a part of a big programming system. It can be used, for instance, for dragging and dropping of objects specific to the system above.
The task is to create the "AhCustomDragDrop.dot" template and the "AhCustomDragDrop.dll" library, which together demonstrate the possibility of Drag-n-Drop customization for Microsoft Word document windows.
The "AhCustomDragDrop" toolbar looks like this:
The brief description of the toolbar buttons is given in the table below.
Brief Prompt on the toolbar buttons looks like this
On pressing the "Examine" button a message appears about whether the current window is registered or not. The message looks like this.
To test the template you should drag and drop some object (a text from NotePad or a file from Windows Explorer) to the Microsoft Word window (no open documents) or the document window. Instead of ordinary action/performance/conduct (inserting of a dropped text or an object to the document or opening of a file) the following message should appear.
Implementation - AhCustomDragDrop.dll library
The library is designed for customizing Drag-n-Dropís behavior of Microsoft Word and is used together with the "AhCustomDragDrop.dot" template.
Since Drag-n-Drop event handlers are related to windows, the library supports the registration of Microsoft Word open document windows.
The library exports the AhDropRegisterWindow, AhDropUnRegisterWindow, AhDropUnRegisterAll and AhDropIsRegisteredWindow functions. The library export file is given below.
C++ Source Code
The C++ source codes are not included in the current edition.
How It Works
In Drag-n-Drop implementation at least two windows take part Ė the source window and the target window. The user presses the mouse button in the source window and keeping the button pressed drags the cursor into the target window where he releases the mouse button. The target window may coincide with the source window. The dragged data is usually associated with the selected objects in the source window but it is not a must.
The source window reacts to the mouse button pressing and calls the DoDragDrop function. Besides, this window should provide the user with the pointer to the IDropSource and IDataObject interface realization.
The target window provides the user with the pointer to the IDropTarget interface realization. The pointer should be preliminarily registered in the system by calling the RegisterDragDrop function.
The RegisterDragDrop function has the following prototype.
To delete the window registration the RevokeDragDrop function is used which has the following prototype.
Tracking Microsoft Word Document Windows
To track the Microsoft Word document windows you should trace the processes of document windows creation and removal. The new windows creation coincides with the new document creation and with the opening of existing documents. The window deletion coincides with the 'close document' operation.
The simplest way of tracking the mentioned processes is the interception of the application events. That is the document opening event (_DocumentOpen), the new document creation event (_NewDocument) and the document closure event (_DocumentBeforeClose).
To intercept the events the class module is necessary. It should contain the object of the Word.Application class declared with the "WithEvents".
Automatic macros AutoExec and AutoExit are the best way for automatic creation and removal of an application object.
Brief description of the IDropTarget interface functions is given in the following table.
You do not call the methods of IDropTarget directly.
The DoDragDrop function calls the IDropTarget methods during the drag-and-drop operation.
Implementation - Template AhCustomDragDrop.dot
The template contains the "AhCustomDragDrop" toolbar and the corresponding macros.
Class Module "AhWordApplication"
The simple implementation of Microsoft Word Drag-n-Drop customization is given in this etude. It consists of the "AhCustomDragDrop.dot" (VBA) template and the "AhCustomDragDrop.dll" (C++) library.
1. Is it possible to customize Drag-n-Drop behavior for Microsoft Word document window using VBA only?
2. For what personal purpose you may need to change standard Microsoft Word document window Drag-n-Drop behavior?
Etudes for Microsoft
otherwise noted, all materials on this site are