Eome

Hi,

I would like to insert a custom winform control (usercontrol) into a word document using add-in.

I read this article to perform this but i can't insert the control inline : http://eightpence.com/inserting-windows-forms-controls-into-word-documents-using-com-addins/

My first question is : how can i insrt usercontrol in word document using add-in

My second question concerns VSTO. With controls.AddControl, we easilly insert usercontrol in word docuement, but when i save the document, the usercontrol is not saved.

How save a word document keeping usercontrol

Thanks,

Eome



Re: Visual Studio Tools for Office Word add-in and winform control

Cindy Meister

Hi Eome

It isn't possible to place a WinForms control on a Word document using an Add-in. You can use the full VSTO (not the SE version) to create a document-level customization. Document-level customizations do support placing WinForms controls on Word (and Excel) document. In order to do so, VSTO wraps the controls into COM ActiveX controls. This functionality is not available outside the VSTO designer. You could create your own COM ActiveX controls and insert these into a Word document.

Beyond that, only Word's internal form fields and ActiveX controls are available for you to use.

As to your second question, your observations are correct. VSTO can insert/create host controls at run-time, but these are not saved in the document. You'd need to store the information somewhere, then recreate the controls each time the document is re-opened.






Re: Visual Studio Tools for Office Word add-in and winform control

Eome

Thank you for your answer !

But if you follow the link i gave, you will see how to insert winform control into word using add-in. I tested, it works however, i can't insert my control (registered as COM) in a inline shape (as describe in the article).

Edit : For saving winform controls, there is a problem :

when the document containing usercontrol is saved (file/save as), no code is executed after the next opening (the document is now a classic one). So i can't create the control again.

I really thank you for all your confirmations !

Eome





Re: Visual Studio Tools for Office Word add-in and winform control

Cindy Meister

Eome wrote:

But if you follow the link i gave, you will see how to insert winform control into word using add-in. I tested, it works however, i can't insert my control (registered as COM) in a inline shape (as describe in the article).

And I quote you from the link:

<<Still interested Do you really have a pressing need to put your own managed controls and have them hosted on a word document If so, read on, but clothe thyself in fortitude, and prepare for ghastly workarounds, ill-mannered code and harrowing hacks. This scenario is explicitly not supported by the VSTO team, nor is it documented anywhere at the time of writing.>>

<<it appears these controls can not be hosted as an InlineShape on the word document; only as a floating Shape>>

So, firstly the approach is explicitly not supported by the team in this forum. And secondly the author tells you his approach will not work with InlineShapes. And it appears he has researched it extensively. There is a "Comments" section on this page, I suggest you take it up with Mr. Crosby.

Eome wrote:
Edit : For saving winform controls, there is a problem :

when the document containing usercontrol is saved (file/save as), no code is executed after the next opening (the document is now a classic one). So i can't create the control again.

Was the document generated from a VSTO template And then saved to a different location Then there may be a problem with security if you've used location-based CASPOL.






Re: Visual Studio Tools for Office Word add-in and winform control

Cindy Meister

Hi Eome

<< i can't insert my control (registered as COM) in a inline shape (as describe in the article).>>

Further thoughts...

You might try searching/asking in the office.developer.* newsgroups. I know there are people who've attempted putting their own ActiveX controls (not necessarily managed code) into Word docs and have run into all sorts of problems. Perhaps something they mention will trigger an idea for you.

Another place to ask would be in the net.framework.interop newsgroup. Someone there might have an idea, although since the problem seems to be with an idiosyncracy of Word, I'm not hopeful.






Re: Visual Studio Tools for Office Word add-in and winform control

Eome

Thank you Smile

I'll try to post on theses newsgroups





Re: Visual Studio Tools for Office Word add-in and winform control

Geoff Darst - MSFT

Hi Eome,

The problem with the WinForms ActiveX implementation is that it is far from complete. It is missing some critical interfaces such as IDataObject--which is why you can't insert as in-line shape. That is also why you can't copy and paste (IIRC Word will crash when you try to do this). I would really caution you against going down this road unless your Add-In is for your personal use only. The combination of Word as an ActiveX container and WinForms as an ActiveX control is an extremely bad one. We had to do an incredible amount of work to overcome the limitations of both--and we were only moderately successful at that. Moreover, the controls scenario is far more complicated than the article you reference suggests. There are all sorts of different ways you can host a control on a Word document and all sorts of ways your users can interact with them. The testing overhead you are signing up for is prohibitive--particularly considering that the only containers that WinForms supports (besides the VSTO container) is Internet Explorer and the MFC control container. No testing of WinForms controls hosted directly in Office has ever been done--other than what I did when I was investigating how to support this feature in VSTO. The results of that (very limited) testing effectively proved that the approach suggested by your article does not work beyond the most basic scenarios.

As to your other question, as Cindy mentions, the dynamic controls model does not save controls to the document. If we did that, you would have to write your code so that you checked to see if the control was already there before inserting it. If you want the control to remain on the document, you should just drag and drop it from the toolbox (your user control should show up there once you build it--or if it is a stand-alone assembly, you can just add it). If you don't want the control to be on the document except for certain circumstances, you would either need to persist enough information to recreate the control yourself (didn't we ship a sample to do that in Whidbey ) or you could just selectively hide and unhide the control by setting the visible property.

Sincerely,

Geoff Darst

Microsoft VSTO Team