Hello Dave,
First I want to make sure I understand this issue correctly. From the
description, we create our own IDataObject object. When we drag an instance
of our IDataObject object into Word document, Word will call our
IDataObject.ClipboardText() method. The issue is that in the
IDataObject.ClipboardText() method, we cannot know where the drop action
occurs, right? If I have misunderstood, please feel free to correct me.
After some research, I find a workaround with a Timer. Using a timer is not
a robust solution in this scenario, I know. So, I am still working on this
issue to see if I can find a better solution. I also consulted the product
team to see whether they had any good ideas on this. I will let you know
when I have any new findings or get any results from the product team.
The following is the detailed information of the workaround I mentioned
above. First, we can create a timer and use its Enable property to control
how its Tick event fires. Every time we drag an IDataObject object into the
Word document, Word will call the IDataObject.GetData() method. There, we
set the timer's Enable property to true. So, the Tick event will fire. In
the Tick event's handle function, we call Application.DoEvents() to ensure
all the messages in the message queue are processed. Consequently, at this
time the IDataObject object is already pasted into the Word document. So,
we can easily get the drop location from Word.Application.Selection.Start.
If the drop location is where we want to perform the drop action, we do
nothing, if not, we call Application.ActiveDocument.Undo() method to cancel
the drop action.
As far as I test, the following codes work fine on my side:
class myDataObject: IDataObject
{
Timer t;
public myDataObject(String s)
{
t = new Timer();
t.Enabled = false;
t.Interval = 1;
t.Tick += new EventHandler(t_Tick);
this.SetData(s);
}
void t_Tick(object sender, EventArgs e)
{
t.Enabled = false;
Application.DoEvents();
if (Globals.ThisAddIn.Application.Selection.Start == 4)
{
object time = 1;
Globals.ThisAddIn.Application.ActiveDocument.Undo(ref time);
}
}
public object GetData(string format, bool autoConvert)
{
t.Enabled = true;
return Clipboard.GetData("String");
}
public void SetData(object data)
{
Clipboard.SetData("String", data);
}
......
}
And I use this line of codes to trigger the drag and drop:
this.label1.DoDragDrop(new
myDataObject(this.label1.Text),DragDropEffects.Copy);
Hope it helps!
Best regards,
Ji Zhou (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
This posting is provided "AS IS" with no warranties, and confers no rights.