views:

121

answers:

1

Hi!

I am developing a template based addin for Word 2003 which allows the user to drag and drop elements from a listbox into the word document. Unfortunately I'm getting a really strange behaviour when trying to drop elements in the document's header.

  1. Open the template and type something in the header
  2. Close the header and insert some content on the page
  3. Add a page break.
  4. Switch to page layout mode where and set zoom level to "Two Pages"
  5. Open the header
  6. Slowly Drag and Drop an list item from the list box to the header.
  7. See multiple Page Setups dialogs occur which cause Word to crash.

Here is my code:

// in ThisDocument.cs

public MyUserControl _control;
public void Init()
{
    _control = new MyUserControl();
    ActionsPane.Controls.Add(_control);
    ActionsPane.Visible = true;
}

// in MyUserControl.cs

public void listBox1_MouseDown(object sender, MouseEventArgs e)
{
    DoDragDrop("something", DragDropEffects.Copy);
}

Have I done somethinkg wrong with implementing Drag and Drop? Is there a workaround for this strange behaviour?

Thanks in advance,
Oliver Hanappi

Video

I've made a little video which shows the bug. You can download it from here: http://rapidshare.com/files/364907873/word-2003-bug.rar

Unfortunately the tool I used didn't notice that I changed the display settings, so although I used 1280x768, it recorded 1920x1200, so sorry for the strange video size. If you cannot watch the video, maybe the codec is missing. You can get it here: http://camstudio.org/

I also forgot to mention that the bug also occurs in Word 2007. Because on my local machine I have only Word 2007, I recorded the video with Word 2007, but it is basically the same for Word 2003 (where I found the bug)

+1  A: 

This worked for me in Word 2007, but it seems like an awful work-around. Try using the clipboard to set a keyword in the document, then handle the WindowSelectionChange event that seems to fire reliably after text insertion to do what you really want to get done.

public void listBox1_MouseDown(object sender, MouseEventArgs e)
{
    Clipboard.SetText("<#KEYWORD#>");
    IDataObject dObject = Clipboard.GetDataObject();

    //This is extremely buggy coming from VSTO, this is why the clipboard is used.
    DoDragDrop(dObject, DragDropEffects.All);
}

void Application_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)
{
    if (Sel.Range.Text == "<#KEYWORD#>")
    {
        Sel.Range.Text = string.Empty;
        // Do some action
    }
}
Mike Regan
It still does not seem to work every time I try it, but I am going to test it when in office. Btw, I've made a little video showing the bug if someone cannot reproduce it.
Oliver Hanappi