none
Creating first content control? Very confused by naming RRS feed

  • Question

  • Hey there,

    first of all I would like to apologize for my probably very basic question, but I really got stuck.

    Lets assume I have a very basic wpf application, i.e., a very simple drawing tool like paint. Everything is inside a WPF user control. This works as a stand-alone application (when embedded in a window). I log my changed made and write them in a file when the application is closed and parse this file when reopening to have the same state as when closed.

    Now I would like to place one or more of these controls inside a word, excel or powerpoint application (2013), by means of placing, copy & pasting and so on like an excel diagram. In addition, these user controls should be stored when the document is saved and reloaded again (using the user controls constructor and destructor).

    I tried around some time and come with an "quite close" solution, but I dont understand many things, such that I dont know, if I am on the correct trip.

    What I did:

    - Create VSTO Excel Add-In Project

    - Added my application as custom WPF control

    - Added a Windows Forms control that hosts the WPF control (elementHost)

    - Create a ribbon with a single button. When clicked, my code:

    Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDocument.Controls.AddControl(new myWindowsFormsUserControl(), 10, 20, 100, 100, "myControl");

    This works by part, since I have my control in Excel and use the functions given by my application.

    What does NOT work?

    1) I cannot resize, move or copy&paste the control by any chance. It is fixed where it is.

    2) The control is not stored correctly with the worksheet. After opening, the control is only a rastatized image and my control's functions are gone.

    3) I can only have one control, since the name has to be unique I guess. Of course I could implement a counter and change the string with each click, but that only seems to be a workaround.

    Does anyone have some help?

    kind regards,

    curator


    Friday, May 29, 2015 1:36 PM

Answers

  • Hi curator

    I'm going to move your issue to the more appropriate VSTO forum, however...

    The issues you ask about are known limitations of controls on a document surface.

    For (2), read the VSTO documentation carefully - it describes how your code must store the control state when the workbook is closed, then re-instate it when the workbook is re-opened.

    You see, Excel doesn't know how to handle what's inside the control. All it "sees" is the ActiveX container VSTO wraps your control in - that static image. All VSTO controls added at Design time are handled like this by the automatic VSTO code. For Controls you add dynamically you have to handle the coding.

    https://msdn.microsoft.com/en-us/library/cc442765(v=vs.140).aspx


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, May 29, 2015 5:46 PM
    Moderator
  • <<However, it is important that I can move and resize the control. Is there no way to allow that?>>

    If you use dynamic controls it might be possible for your code to destroy one it's created and re-create it in another location.

    As I recall (it's been sometime since I worked with this aspect, so things are a bit hazy), the problem you have with controls added at Design time is that VSTO automatically creates classes for the objects. When you move or cut/paste the Office application actually DELETES and recreates something. This would play havoc with VSTO's trying to manage the objects, so it's not allowed. But the same restriction shouldn't apply to controls you handle completely with your code - at least, as far as you handling the modifications to the controls.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 1, 2015 5:54 PM
    Moderator

All replies

  • Hi curator

    I'm going to move your issue to the more appropriate VSTO forum, however...

    The issues you ask about are known limitations of controls on a document surface.

    For (2), read the VSTO documentation carefully - it describes how your code must store the control state when the workbook is closed, then re-instate it when the workbook is re-opened.

    You see, Excel doesn't know how to handle what's inside the control. All it "sees" is the ActiveX container VSTO wraps your control in - that static image. All VSTO controls added at Design time are handled like this by the automatic VSTO code. For Controls you add dynamically you have to handle the coding.

    https://msdn.microsoft.com/en-us/library/cc442765(v=vs.140).aspx


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, May 29, 2015 5:46 PM
    Moderator
  • Hey, thanks for helping.

    I'll try that nd it seems (2) is solvable. And for (3), it might not been nice but one can have a map for each document. However, it is important that I can move and resize the control. Is there no way to allow that?

    regrads,

    curator

    Sunday, May 31, 2015 7:29 PM
  • <<However, it is important that I can move and resize the control. Is there no way to allow that?>>

    If you use dynamic controls it might be possible for your code to destroy one it's created and re-create it in another location.

    As I recall (it's been sometime since I worked with this aspect, so things are a bit hazy), the problem you have with controls added at Design time is that VSTO automatically creates classes for the objects. When you move or cut/paste the Office application actually DELETES and recreates something. This would play havoc with VSTO's trying to manage the objects, so it's not allowed. But the same restriction shouldn't apply to controls you handle completely with your code - at least, as far as you handling the modifications to the controls.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 1, 2015 5:54 PM
    Moderator