Drag and Drop Win Forms to Word Document

Add-in Express™ Support Service
That's what is more important than anything else

Drag and Drop Win Forms to Word Document
interactiv drag and drop from treeview to a random word document and creating bookmarks  
Thomas Schweiger




Posts: 5
Joined: 2015-06-08
Ladies and gentlemen,

I try to generate a drag and drop functionality from an treeview win form into a word document under Word 2016.
All is well except that i lose the focus of the activ word document from time to time.
This cases the loss of the current cursor position and the user ends up droping the content in a different place as the mouse points to.

If i move the mouse outside of the activ word document and back again i will get the view of the cursor position back. Which moves around if you move the mouse around and showes the insertion point of the element (in my case a bookmark)

As mentioned in another topic in this forum i use WindowSelectionChange to catch the drop event in word.
This works well exept of the problem with the currsor position.

Does anyone know how to regain the focus of the activ word document to see the cursor position without moving the mouse outside the activ word document?

Thank you in anticipation

cLS
Posted 10 Aug, 2016 03:54:39 Top
Datascout Datascout


Guest


Hello Thomas,

I had a similar issue. I've also got an Word add-in with a treeview users can enter items into a document :)

After you enter the treeview item into the current selection, I found setting focus back to the active window fixed the issue, like this:


sel.TypeText(value)
wd.ActiveWindow.SetFocus()


Where sel is a reference to a Word Selection object and wd is a reference to the Word Application.

Let me know how it goes.
Posted 10 Aug, 2016 08:10:21 Top
Thomas Schweiger




Posts: 5
Joined: 2015-06-08
Hi Datascout Datascout,

Thank you for your answer I tried it but it wouldn't do the job.

It works from time to time and then loses the focus again.

Any other solutions

cLS
Posted 11 Aug, 2016 02:45:37 Top
Datascout Datascout


Guest


Hi Thomas,

I'm out of ammunition I'm afraid. That approach worked perfectly for my circumstances.

Perhaps Andrei & Team may have other ideas.

Cheers - Marcus
Posted 11 Aug, 2016 05:41:40 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Marcus,

Thank you for sharing your knowledge!

Hello Thomas,

Thank you for sending us the project.

I've noticed two potential sources of the problem.

First off, you leave a number of COM object unreleased. This isn't a best practice when dealing with Office. You should understand that 1) Office was designed when the creation of the .NET Framework wasn't even discussed; 2) *all* of the developer tools available at those times released COM objects automatically (C/C++ developers are aware of the need to release COM objects manually). With .NET languages, *you* are responsible for releasing COM object. Although Word is usually tolerant to unreleased COM objects there's no guarantee that all Word versions/updates in all situations behave in the way assumed in the code of your add-in. I suggest that you check section Releasing COM objects at https://www.add-in-express.com/docs/net-office-tips.php#releasing.

Second. You change the selection in the event which is triggered by changing the selection. I suppose each of your manipulations may trigger a new WindowSelectionChange. Whether this is so or not, I suggest that you a) use the selection the event handler provides instead of retrieving the selection yourself and b) modify the selection *after* the WindowSelectionChange is complete; see section Wait a little on the same page. You may need to invent a mechanism preventing new

Finally, do not use MessageBox.Show() to debug your code. Showing/hiding a message box triggers the Activate/Deactivate events on the underlying Office host application window and the consequent chain of eventsspoils the picture. Use System.Diagnostics.Debug.WriteLine() instead. You collect these messages at run time in the debug windows of VS. Outside of a debug session, you use the DebugView utility; see http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx.


Andrei Smolin
Add-in Express Team Leader
Posted 12 Aug, 2016 06:13:59 Top
Thomas Schweiger




Posts: 5
Joined: 2015-06-08
Hello Andrei Smolin,

Thank you for your quick response.

To your first comment it is right that i didn't release the COM Objects I tried
        
If oWord IsNot Nothing Then
   Marshal.ReleaseComObject(oWord)
End If
If oWordDoc IsNot Nothing Then
      Marshal.ReleaseComObject(oWordDoc)
End If

after the WindowSelectionChange Handler but it didn't change the behavior.(Am i getting you right i shouldn't change the content of the document in the handler)

I can't close the document by oWordDoc.Close() and oWord.Quit() I also can't create a oWordDoc = new Word.Document because i have to work with the already loaded instance oWord = Marshal.GetActiveObject("Word.Application") I will change the content of the word document by creating
bookmarks on diffrent places in the word document.

To your second statement: "I change the selection in the event which is triggered by changing the selection" Is there another way to catch an event by a drag and drop operation from a treeview to an word document except


oWord_WindowSelectionChange(ByVal sel As Word.Selection) Handles oWord.ApplicationEvents3_Event_WindowSelectionChange

?
"a) use the selection the event handler provides instead of retrieving the selection yourself "


and to the last statement yes you are right to debug i should use the Debug.Writline() call i normally do.

It would be great if you could help me find the right handler for such a task ( Drag & Drop out of a treeview and into a word document by creating an bookmark on the cursor position in the document) and not lose the focus (blinking cursor) of the document while droping the content into the word document.

I hope i am not totally on the wrong path.

Best Regards

cLS
Posted 16 Aug, 2016 04:27:04 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Thomas,

It looks like using the WindowSelectionChange event is a correct way. Still, you need to retrieve the selection from the object passed to by the event handler. Also, I would create a simple add-in showing a treeview and intercepting WindowSelectionChange and having no other Word-related code code and check if the issue is reproducible on this add-in.


Andrei Smolin
Add-in Express Team Leader
Posted 16 Aug, 2016 05:09:31 Top