none
Re-Creating Windows Forms Controls When Documents Are Opened RRS feed

  • Question

  • Hello,

    I've created a VSTO Word Add-In that allow me to add a checkbox control to document at run time when something appens.

    Dim chCheckBoxControl As New CheckBox
    Dim objCheck As Microsoft.Office.Tools.Word.ControlSite
    
    objCheck = Documento.Controls.AddControl(chCheckBoxControl, Range, 10, 10, "CheckBoxControl" & iIndexCheckBoxControl)
    


    This work fine, but when the document is saved and then closed, the Visual Studio Tools for Office runtime automatically removes the created checkbox from the document.

    I need to store information about it's size and location to a XML part in the document, in order to re-create the checkbox when the document reopens.

    Befone close the document i'm seaching on the document controls collection to find all CheckBox's controls

    For Each control As Object In vstoDocument.Controls
       If TypeOf control Is System.Windows.Forms.Control Then
          If TypeName(control) = "CheckBox" Then
    

    How can i get information about the checkbox's location in the document?

    Thursday, November 3, 2011 6:01 PM

Answers

  • Hi Ari

    (For Tom: the subject under discussion is here in the MSDN documentation: http://msdn.microsoft.com/en-us/library/cc442765.aspx)

    If you carefully read the information in the MSDN documentation you'll see that Word "sees" content controls as ActiveX objects on the document surface - Word has no idea what  a Windows Form control is, but it does recognize the right kind of ActiveX objects.

    However, Word won't give you a collection of ActiveX objects, either. It considers them to be a subset of graphical objects, as members of the InlineShapes or Shapes collection. By default, when you insert a Windows Forms control it will be a member of the InlineShapes collection; if you apply text flow formatting (wrap the text around the control in some way) it will be switched to the Shapes collection.

    For a member of the InlineShapes collection the RANGE object is what holds the location. A Word Range object cannot be serialized to XML, however. You might think you could use the Range's Start and End properties, but these are notoriously volatile and I wouldn't recommend that.

    Instead, it would probably be best to insert a BOOKMARK object just before the control. Store the bookmark name in the custom xml part and use that to derive the Range object for re-creating the control. Along these lines: Dim rngControlTarget as Word.Range = Documento.Bookmarks("bookmarkName").Range


    Cindy Meister, VSTO/Word MVP
    Tuesday, November 8, 2011 10:12 AM
    Moderator

All replies

  • Hi Ari,

     

    Thanks for posting in the MSDN Forum.

     

    Would you please clarify the sentence of <<objCheck = Documento.Controls.AddControl(chCheckBoxControl,Range,10,19,”CheckBoxControl”&iIndexCheckBoxControl) >>? It’s based on my experience Document doesn’t have Controls property. Do you mean ContentControl property here?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 8, 2011 2:18 AM
    Moderator
  • Hi Ari

    (For Tom: the subject under discussion is here in the MSDN documentation: http://msdn.microsoft.com/en-us/library/cc442765.aspx)

    If you carefully read the information in the MSDN documentation you'll see that Word "sees" content controls as ActiveX objects on the document surface - Word has no idea what  a Windows Form control is, but it does recognize the right kind of ActiveX objects.

    However, Word won't give you a collection of ActiveX objects, either. It considers them to be a subset of graphical objects, as members of the InlineShapes or Shapes collection. By default, when you insert a Windows Forms control it will be a member of the InlineShapes collection; if you apply text flow formatting (wrap the text around the control in some way) it will be switched to the Shapes collection.

    For a member of the InlineShapes collection the RANGE object is what holds the location. A Word Range object cannot be serialized to XML, however. You might think you could use the Range's Start and End properties, but these are notoriously volatile and I wouldn't recommend that.

    Instead, it would probably be best to insert a BOOKMARK object just before the control. Store the bookmark name in the custom xml part and use that to derive the Range object for re-creating the control. Along these lines: Dim rngControlTarget as Word.Range = Documento.Bookmarks("bookmarkName").Range


    Cindy Meister, VSTO/Word MVP
    Tuesday, November 8, 2011 10:12 AM
    Moderator
  • HI Cindy,

    Thanks for you great work. I think what you said is very helpful for this issue.

    @Ari,

    Do you have solved your issue?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 10, 2011 10:00 AM
    Moderator