none
change customxml linked with a content control RRS feed

  • Question

  • Hi, if you change a customxml, linked with a content control, in runtime, it's necesary close and open de document to see changes in your content control ?????

    Note: I have changed customxml with interop word

    Wednesday, March 21, 2012 12:00 PM

All replies

  • Hi RamonMR,

    Thank you for posting.

    I tested on my side via vba, however, the document does not need to be closed and reopened on my side. I use the following code snippet:

    Sub AddCustomXMlPart()
    If ActiveDocument.CustomXMLParts.Count = 4 Then
     ActiveDocument.CustomXMLParts(4).Delete
     ActiveDocument.CustomXMLParts.Add
     ActiveDocument.CustomXMLParts(4).Load ("C:\Users\v-bpeng\Desktop\MapContentControl\CustomLetter.xml")
    Else
     ActiveDocument.CustomXMLParts.Add
     ActiveDocument.CustomXMLParts(4).Load ("C:\Users\v-bpeng\Desktop\MapContentControl\CustomLetter.xml")
    End If
    End Sub
    Sub MapXml()
    Dim strXPath1 As String
    strXPath1 = "/Customer/CompanyName"
    ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1
    
    Dim strXPath2 As String
    strXPath2 = "/Customer/ContactName"
    ActiveDocument.ContentControls(2).XMLMapping.SetMapping strXPath2
    
    Dim strXPath3 As String
    strXPath3 = "/Customer/ContactTitle"
    ActiveDocument.ContentControls(3).XMLMapping.SetMapping strXPath3
    
    Dim strXPath4 As String
    strXPath4 = "/Customer/Phone"
    ActiveDocument.ContentControls(4).XMLMapping.SetMapping strXPath4
    End Sub

    So, I think it possible that you didn't delete the original xml file before inserting the new one. 

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 23, 2012 3:03 AM
  • Hi Bruce,

    Thank you for reply.

    I haven't had any problems to add nuevos customxmls. Mi problem is when I want to change the property text from customxml. This is my code:

    ControlesContenido.vb

    Imports Microsoft.Office.Core
    Imports Formateadores
    Imports Microsoft.Office.Interop.Word
    Imports System.Text
    Imports System.IO
    
    Namespace EntidadesWord
    
        ''' <summary>
        ''' Clase que encapsula la operativa sobre los controles de contenido dentro del documento
        ''' </summary>
        ''' <remarks></remarks>
        Public Class ControlesContenido
            Implements IDisposable
    
            Protected document As Documento
            Private persistido As Boolean
    
            Public Sub New(ByVal d As Documento)
                Me.document = d
                Me.persistido = True
                Me._xmlDoc = New Xml.XmlDocument
            End Sub
    
    
            Public Sub AddControlContenido(ByVal c As IControlContenido)
                Dim parentNode As Xml.XmlNode
                Dim childNode As Xml.XmlNode
                Dim nodoExistente As Boolean = False
    
                parentNode = Me.GetRootNode
    
                If c.securizado AndAlso parentNode IsNot Nothing Then
    
                    Dim newChild As Xml.XmlNode
                    newChild = _xmlDoc.CreateNode(Xml.XmlNodeType.Element, "Control", Nothing)
                    Dim at As Xml.XmlAttribute = _xmlDoc.CreateAttribute("GUID")
                    at.Value = c.Id
                    newChild.Attributes.Append(at)
                    Dim nodeAux As Xml.XmlNode
                    With c.Binding
                        nodeAux = _xmlDoc.CreateNode(Xml.XmlNodeType.Element, "BCCTipo", Nothing)
                        nodeAux.InnerText = .Tipo
                        newChild.AppendChild(nodeAux)
                        nodeAux = _xmlDoc.CreateNode(Xml.XmlNodeType.Element, "BCCPropiedad", Nothing)
                        nodeAux.InnerText = .Propiedad
                        newChild.AppendChild(nodeAux)
                        nodeAux = _xmlDoc.CreateNode(Xml.XmlNodeType.Element, "BCCIdEntidad", Nothing)
                        nodeAux.InnerText = .IdEntidad
                        newChild.AppendChild(nodeAux)
                        nodeAux = _xmlDoc.CreateNode(Xml.XmlNodeType.Element, "BCCFormato", Nothing)
                        nodeAux.InnerText = .Formato
                        newChild.AppendChild(nodeAux)
                        nodeAux = _xmlDoc.CreateNode(Xml.XmlNodeType.Element, "Value", Nothing)
                        'nodeAux.InnerText = c.Text
                        nodeAux.InnerText = "" ' Al hacer commit changes es cuando obtenemos el valor del texto
                        newChild.AppendChild(nodeAux)
                    End With
    
                    childNode = parentNode.SelectSingleNode("//Control[@GUID=" & c.Id & "]")
    
                    If childNode Is Nothing Then
                        parentNode.AppendChild(newChild)
                    Else
                        parentNode.ReplaceChild(newChild, childNode)
                        nodoExistente = True
                    End If
                    Me.persistido = False
                End If
    
                If Not nodoExistente Then
                    Me.Controles.Add(c)
                End If
    
            End Sub
    
    
            Public Sub Delete(ByVal c As IControlContenido)
                Dim parentNode As Xml.XmlNode
                Dim childNode As Xml.XmlNode
                If c IsNot Nothing Then
                    parentNode = Me.GetRootNode
    
                    If c.securizado And parentNode IsNot Nothing Then
    
                        childNode = parentNode.SelectSingleNode("//Control[@GUID=" & c.Id & "]")
    
                        If childNode IsNot Nothing Then
                            parentNode.RemoveChild(childNode)
                            Me.persistido = False
                        End If
                    End If
                    Me.Controles.Remove(c)
                End If
            End Sub
    
            Private ControlesABorrar As IList(Of IControlContenido) = New List(Of IControlContenido)
    
            Private Sub ControlContenidoDeleted(ByVal Cc As IControlContenido)
                Me.Delete(Cc)
            End Sub
    
    
            Public Sub Delete(ByVal id As String)
                Me.Delete(Me.Controles.FirstOrDefault(Function(c) c.Id = id))
            End Sub
    
    
            Private _ListaControlesContenido As IList(Of IControlContenido) = Nothing
    
            Protected ReadOnly Property Controles As IList(Of IControlContenido)
                Get
                    If _ListaControlesContenido Is Nothing OrElse _ListaControlesContenido.Count = 0 Then
                        _ListaControlesContenido = New List(Of IControlContenido)
    
                        Dim rootNode As Xml.XmlNode = Me.GetRootNode
                        If rootNode IsNot Nothing Then
                            Dim control As IControlContenido
                            Dim listaNodos As Xml.XmlNodeList
                            Dim nodoGUID As Xml.XmlNode
    
                            listaNodos = rootNode.SelectNodes("//Control")
    
                            For Each node As Xml.XmlNode In listaNodos
                                nodoGUID = node.Attributes.OfType(Of System.Xml.XmlAttribute).FirstOrDefault(Function(n) n.Name.Equals("GUID"))
                                If nodoGUID IsNot Nothing Then
                                    control = New ControlContenido(Me.document)
                                    control.Id = nodoGUID.InnerText
                                    For Each nodeChild As Xml.XmlNode In node.ChildNodes
                                        Select Case nodeChild.Name
                                            Case "BCCTipo"
                                                control.Binding.Tipo = nodeChild.InnerText
                                            Case "BCCIdEntidad"
                                                control.Binding.IdEntidad = nodeChild.InnerText
                                            Case "BCCPropiedad"
                                                control.Binding.Propiedad = nodeChild.InnerText
                                            Case "BCCFormato"
                                                control.Binding.Formato = nodeChild.InnerText
                                        End Select
                                    Next
                                    control.securizado = True
                                    _ListaControlesContenido.Add(control)
                                End If
                            Next
                        End If
                       
    
                            AddHandler Me.document.InternalDocument.XMLBeforeDelete, AddressOf TratarBeforeDeleteXMLNode
                            AddHandler Me.document.InternalDocument.ContentControlBeforeDelete, AddressOf TratarBeforeDeletedContentControl
    
                        End If
                    End If
                    Return _ListaControlesContenido
                End Get
            End Property
    
    
            Public Sub InsertControlContenido(ByVal binding As BindingControlContenido, ByVal value As String)
                Dim ct As ContentControl
                If Me.document.Abierto Then
    
                    With Me.document.Selection
                        .Start = .End
                        Dim inicio As Integer = .Start
                        WordUtil.EscribirTextoComando_ModoAntiguo(Me.document, value)
                        .Start = inicio
                        Select Case binding.ValueBehavior
                            Case BehaviorsValues.Fecha
                                ct = Me.document.InternalDocument.ContentControls.Add(WdContentControlType.wdContentControlDate, .Range.InternalRange)
                            Case BehaviorsValues.Lista
                                ct = Me.document.InternalDocument.ContentControls.Add(WdContentControlType.wdContentControlComboBox, .Range.InternalRange)
                            Case Else
                                ct = Me.document.InternalDocument.ContentControls.Add(WdContentControlType.wdContentControlText, .Range.InternalRange)
                        End Select
                        ct.Tag = ct.GetHashCode
                        ct.LockContents = True
                        ct.LockContentControl = True
                    End With
    
                    AddControlContenido(New ControlContenido(Me.document) With {.Id = ct.GetHashCode, .Binding = binding, .Securizado = True})
                End If
            End Sub
    
    
            Protected Sub InsertText(ByVal s As String, ByVal c As IControlContenido)
                Try
                    If Me.document.Abierto Then
                        Me.document.Desproteger()
                        If TypeOf c Is ControlContenido Then
                            c.readOnly = False
                            c.Text = s
                            c.readOnly = True
                            Logger.Escribir(" ControlContenido Actualizado ")
                        End If
                    End If
                Catch ex As Exception
                    Logger.EscribirException(ex)
                End Try
    
            End Sub
    
            Private _internalRootNode = Nothing
            Private _xmlDoc As Xml.XmlDocument
    
            ''' <summary>
            ''' Nodo Principal interno para crear una estructura en memoria del xml resultante que se debe guardar en el documento
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Private ReadOnly Property GetRootNode As Xml.XmlNode
                Get
                    If _internalRootNode Is Nothing Then
                        'Proceso de inicialización
    
                        _xmlDoc = New Xml.XmlDocument
                        Dim xmlDeclaration As Xml.XmlDeclaration = Nothing
                        If Me.document.Abierto Then
                            Dim XmlNode As CustomXMLNode = Nothing
                            For Each part As CustomXMLPart In Me.document.InternalDocument.CustomXMLParts
                                XmlNode = part.SelectSingleNode("//Notin")
                                If XmlNode IsNot Nothing Then
                                    Exit For
                                End If
                            Next
                            If XmlNode Is Nothing Then
                                xmlDeclaration = _xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "")
                                _internalRootNode = _xmlDoc.CreateNode(Xml.XmlNodeType.Element, "Notin", Nothing)
                                _xmlDoc.AppendChild(xmlDeclaration)
                                _xmlDoc.AppendChild(_internalRootNode)
                                Me.persistido = False
                            Else
                                _xmlDoc.LoadXml(XmlNode.OwnerPart.XML)
                                _internalRootNode = _xmlDoc.SelectSingleNode("//Notin")
                            End If
                        Else
                            'TODO posteriormente se hará con OpenXml
                            xmlDeclaration = _xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "")
                            _internalRootNode = _xmlDoc.CreateNode(Xml.XmlNodeType.Element, "Notin", Nothing)
                            _xmlDoc.AppendChild(xmlDeclaration)
                            _xmlDoc.AppendChild(_internalRootNode)
                            Me.persistido = False
                        End If
                    End If
                    Return _internalRootNode
                End Get
            End Property
    
    
            Private Function RefreshNodes() As Boolean
                Dim listaNodos As Xml.XmlNodeList
                Dim childNode As Xml.XmlNode = Nothing
                Dim atribute As Xml.XmlAttribute = Nothing
                Dim control As IControlContenido = Nothing
                Dim result As Boolean = False
                Try
                    listaNodos = _xmlDoc.SelectNodes("//Control")
    
                    If listaNodos IsNot Nothing AndAlso listaNodos.Count > 0 Then
                        result = True
    
    
                        For Each node As Xml.XmlNode In listaNodos
                            childNode = node.ChildNodes.OfType(Of Xml.XmlNode).FirstOrDefault(Function(n) n.Name.Equals("Value"))
                            atribute = node.Attributes.OfType(Of Xml.XmlAttribute).FirstOrDefault(Function(a) a.Name.Equals("GUID"))
                            If atribute IsNot Nothing Then
                                control = Controles.FirstOrDefault(Function(c) c.Id.Equals(atribute.Value) And c.securizado = True)
                            End If
                            If childNode IsNot Nothing AndAlso control IsNot Nothing Then
                                childNode.InnerText = control.Text
                                result = result And True
                            Else
                                result = result And False
                            End If
                            childNode = Nothing
                            atribute = Nothing
                            control = Nothing
                        Next
                    End If
                Catch ex As Exception
                    result = False
                End Try
    
                Return result
            End Function
    
            Public Sub CommitChanges()
                Dim XmlNode As CustomXMLNode = Nothing
                Dim refresh As Boolean
                If Me.document.Abierto And Not persistido Then
                    refresh = RefreshNodes()
    
                    For Each part As CustomXMLPart In Me.document.InternalDocument.CustomXMLParts
                        XmlNode = part.SelectSingleNode("//Notin")
                        If XmlNode IsNot Nothing Then
                            Exit For
                        End If
                    Next
    
                    If XmlNode Is Nothing Then
                        Me.document.InternalDocument.CustomXMLParts.Add(_xmlDoc.InnerXml)
                    Else
    
                        Dim listaCustomXmlNodes As CustomXMLNodes
    
                        listaCustomXmlNodes = XmlNode.SelectNodes("//@GUID")
                        If listaCustomXmlNodes IsNot Nothing AndAlso listaCustomXmlNodes.Count > 0 Then
                            Dim value As String = ""
    
                            For Each controlnode As CustomXMLNode In listaCustomXmlNodes
                                value = controlnode.NodeValue
                                If Me.Controles.Where(Function(c) c.Id.Equals(value)).Count = 0 Then
                                    controlnode.ParentNode.Delete()
                                End If
                            Next
    
                            'Dim nodecustomxml As CustomXMLNode
                            'For indice As Integer = 0 To listaCustomXmlNodes.Count - 1
                            '    nodecustomxml = listaCustomXmlNodes.Item(indice)
                            '    value = nodecustomxml.NodeValue
                            '    If Me.Controles.Where(Function(c) c.Id.Equals(value)).Count = 0 Then
                            '        nodecustomxml.ParentNode.Delete()
                            '    End If
                            'Next
                        End If
    
                        Dim nodoCustom As CustomXMLNode
                        Dim nodoXML As Xml.XmlNode
    
                        For Each control As IControlContenido In Me.Controles.Where(Function(c) c.securizado).ToList
                            nodoCustom = XmlNode.SelectSingleNode("//Control[@GUID=" & control.Id & "]")
                            If nodoCustom Is Nothing Then
                                nodoXML = _xmlDoc.SelectSingleNode("//Control[@GUID=" & control.Id & "]")
                                XmlNode.AppendChildSubtree(nodoXML.OuterXml)
                            End If
                        Next
                    End If
    
                    If Not BindearControlesToCustomXml() Then
                        Logger.Escribir("El bindeo de controles de contenido no ha funcionado", Logger.TipoLineaLog.Info)
                    End If
    
                    persistido = True
                    _internalRootNode = Nothing
                End If
            End Sub
    
            ''' <summary>
            ''' Establece una relación entre los controles de contenido y un custom xml
            ''' <seealso>http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.xmlmapping.setmappingbynode.aspx</seealso>
            ''' </summary>
            ''' <remarks></remarks>
            ''' <returns >Devuelve el exito o no de la operación de bindeo</returns>
            Private Function BindearControlesToCustomXml() As Boolean
                Dim result As Boolean = False
                Dim XmlNode As CustomXMLNode = Nothing
    
                For Each part As CustomXMLPart In Me.document.InternalDocument.CustomXMLParts
                    XmlNode = part.SelectSingleNode("//Notin")
                    If XmlNode IsNot Nothing Then
                        result = True
                        For Each control As ContentControl In Me.document.InternalDocument.ContentControls
                            result = result And control.XMLMapping.SetMapping("//Control[@GUID=" & control.Tag & "]/Value", Source:=part)
                        Next
                        Exit For
                    End If
                Next
                Return result
            End Function
    
    #Region "Tratar eventos delete control contenido"
    
            Private Sub TratarBeforeDeleteXMLNode(ByVal range As Microsoft.Office.Interop.Word.Range, ByVal node As Microsoft.Office.Interop.Word.XMLNode, ByVal InUndoRedo As Boolean)
                Logger.Escribir("Tratar Before Delete XmlNode", Logger.TipoLineaLog.StartProcess)
                If Me._ListaControlesContenido IsNot Nothing AndAlso Me._ListaControlesContenido.Count > 0 Then
                    Dim tag As String = ""
                    Dim idEntity As String = ""
                    For Each n As Microsoft.Office.Interop.Word.XMLNode In node.Attributes
                        Select Case n.BaseName.ToUpper
                            Case EtiquetaXML.TAG_ATTRIBUTE_NAME
                                tag = n.Text
                            Case EtiquetaXML.IDENTITY_ATTRIBUTE_NAME
                                idEntity = n.Text
                        End Select
                    Next
                    Dim cc As IControlContenido = Me._ListaControlesContenido.FirstOrDefault(Function(co) tag.Equals(co.Id) AndAlso idEntity.Equals(co.Binding.IdEntidad))
                    Me.ControlContenidoDeleted(cc)
                End If
                Logger.Escribir("Tratar Before Delete XmlNode", Logger.TipoLineaLog.EndProcess)
            End Sub
    
            Private Sub TratarBeforeDeletedContentControl(ByVal cc As Microsoft.Office.Interop.Word.ContentControl, ByVal InUndoRedo As Boolean)
                Dim contentcontrol As IControlContenido = Me._ListaControlesContenido.FirstOrDefault(Function(co) co.Id.Equals(cc.Tag))
    
                If contentcontrol IsNot Nothing Then
                    Me.ControlContenidoDeleted(contentcontrol)
                End If
            End Sub
    
    
    #End Region
    
            Protected Overridable Sub OnControlContenidoEnter(ByVal c As IControlContenido)
                'RaiseEvent ControlContenidoEnter(c)
            End Sub
    
            Protected Overridable Sub OnControlContenidoExit(ByVal c As IControlContenido, ByVal dirty As Boolean)
                'RaiseEvent ControlContenidoLeave(c)
            End Sub
    
            Public ReadOnly Property ControlesContenidoCollection As IEnumerable(Of IControlContenido)
                Get
                    Return Controles.ToList
                End Get
    
            End Property
    
    
    
    
    #Region "IDisposable Support"
            Private disposedValue As Boolean ' Para detectar llamadas redundantes
    
            ' IDisposable
            Protected Overridable Sub Dispose(ByVal disposing As Boolean)
                If Not Me.disposedValue Then
                    If disposing Then
                        'If d.doc IsNot Nothing Then
                        '    RemoveHandler d.doc.ContentControlOnEnter, AddressOf OnContentControlEnter
                        '    RemoveHandler d.doc.ContentControlOnExit, AddressOf OnContentControlExit
                        'End If
                        Me.document = Nothing
                        Me._ListaControlesContenido = Nothing
                    End If
    
                    ' REVISED: liberar recursos no administrados (objetos no administrados) e invalidar Finalize() below.
                    ' REVISED: Establecer campos grandes como Null.
                End If
                Me.disposedValue = True
            End Sub
    
            ' REVISED: invalidar Finalize() sólo si la instrucción Dispose(ByVal disposing As Boolean) anterior tiene código para liberar recursos no administrados.
            'Protected Overrides Sub Finalize()
            '    ' No cambie este código. Ponga el código de limpieza en la instrucción Dispose(ByVal disposing As Boolean) anterior.
            '    Dispose(False)
            '    MyBase.Finalize()
            'End Sub
    
            ' Visual Basic agregó este código para implementar correctamente el modelo descartable.
            Public Sub Dispose() Implements IDisposable.Dispose
                ' No cambie este código. Coloque el código de limpieza en Dispose (ByVal que se dispone como Boolean).
                Dispose(True)
                GC.SuppressFinalize(Me)
            End Sub
    #End Region
    
        End Class
    End Namespace


    ControlContenido.vb

    Imports Microsoft.Office.Interop.Word
    Imports Microsoft.Office.Core
    
    ''' <summary>
    ''' Clase que encapsula la información sobre la entidad ligada al control, y la posición donde se insertará en el documento. 
    ''' Implementa el patron light-weight
    ''' </summary>
    Public Class ControlContenido
        Implements IControlContenidoList
    
        Private internalDoc As EntidadesWord.Documento = Nothing
        Private internalBinding As BindingControlContenido = Nothing
    
    
        Public Sub New(ByVal d As EntidadesWord.Documento)
            Me.internalDoc = d
            internalBinding = New BindingControlContenido
        End Sub
    
        Private internalListElements As IDictionary(Of String, String) = Nothing
        Public ReadOnly Property ListaElementos As System.Collections.Generic.IDictionary(Of String, String) Implements IControlContenidoList.GetElementos
            Get
                If ValueBehavior = BehaviorsValues.Lista Then
                    If Me.internalListElements Is Nothing OrElse Me.internalListElements.Count = 0 Then
                        Me.internalListElements = New Dictionary(Of String, String)
                        Dim controlInterno As ContentControl = internalContentControl
                        If controlInterno IsNot Nothing Then
                            With controlInterno
                                If .Type = WdContentControlType.wdContentControlComboBox Or .Type = WdContentControlType.wdContentControlDropdownList Then
                                    For Each element As ContentControlListEntry In .DropdownListEntries
                                        Me.internalListElements.Add(element.Text, element.Value)
                                    Next
                                End If
                            End With
                        End If
                    End If
                End If
                Return Me.internalListElements
            End Get
        End Property
    
        Public Function AddElementToList(ByVal StringValue As String, ByVal Value As String) As Boolean Implements IControlContenidoList.AddElementToList
            If ValueBehavior = BehaviorsValues.Lista Then
                Dim controlInterno As ContentControl = internalContentControl
                If controlInterno IsNot Nothing Then
                    With controlInterno
                        If .Type = WdContentControlType.wdContentControlComboBox Or .Type = WdContentControlType.wdContentControlDropdownList Then
                            Try
                                'If .DropdownListEntries.OfType(Of Microsoft.Office.Interop.Word.ContentControlListEntry).
                                '    Count(Function(entry) entry.Text.Equals(StringValue)) = 0 Then
                                .DropdownListEntries.Add(StringValue, Value)
                                'End If
                                Return True
                            Catch ex As Exception
    
                            End Try
                        End If
                    End With
                End If
            End If
            Return False
        End Function
    
        Public Function EstablecerValorInicial(ByVal valor As String) As Boolean Implements IControlContenidoList.EstablecerValorInicial
            Dim elementSelectable As Microsoft.Office.Interop.Word.ContentControlListEntry
    
            If ValueBehavior = BehaviorsValues.Lista Then
                Dim controlInterno As ContentControl = internalContentControl
                If controlInterno IsNot Nothing Then
                    With controlInterno
                        If .Type = WdContentControlType.wdContentControlComboBox Or .Type = WdContentControlType.wdContentControlDropdownList Then
                            Try
                                elementSelectable = .DropdownListEntries.OfType(Of Microsoft.Office.Interop.Word.ContentControlListEntry).
                                    FirstOrDefault(Function(entry) entry.Text.ToLower.Equals(valor.ToLower))
    
                                If elementSelectable IsNot Nothing Then
                                    elementSelectable.Select()
                                    Return True
                                End If
                            Catch ex As Exception
    
                            End Try
                        End If
                    End With
                End If
            End If
            Return False
        End Function
    
        ''' <summary>
        ''' Identificador univoco dentro del documento
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Id As String Implements IControlContenido.Id
    
        ''' <summary>
        ''' Información sobre la entidad ligada al control
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Binding As BindingControlContenido Implements IControlContenido.Binding
            Get
                Return internalBinding
            End Get
            Set(ByVal value As BindingControlContenido)
                internalBinding = value
            End Set
        End Property
    
        Public Property Securizado As Boolean Implements IControlContenido.securizado
    
        ''' <summary>
        ''' Objeto pesado 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Protected Friend ReadOnly Property internalContentControl As ContentControl
            Get
                Try
                    For Each content As Microsoft.Office.Interop.Word.ContentControl In Me.internalDoc.InternalDocument.ContentControls
                        If content.Tag.Equals(Me.Id) Then
                            Return content
                        End If
                    Next
                Catch ex As Exception
                    Formateadores.Logger.EscribirException("Fallo al obtener el objeto pesado de la clase ControlContenido", ex)
                End Try
                Return Nothing
            End Get
        End Property
    
        ''' <summary>
        ''' Metodos equals sobreescrito para comprobacion de tag
        ''' </summary>
        ''' <param name="obj"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            If TypeOf obj Is Microsoft.Office.Interop.Word.ContentControl Then
                Dim tag As String = CType(obj, ContentControl).Tag
                If Me.Id.Equals(tag) Then
                    Return True
                End If
    
            ElseIf TypeOf obj Is ControlContenido Then
                Dim e As ControlContenido = CType(obj, ControlContenido)
                If Me.Id = e.Id Then
                    Return True
                End If
            End If
            Return False
        End Function
    
        Public Property Text As String Implements IControlContenido.Text
            Get
                If Me.internalDoc.Abierto Then
                    Return internalContentControl.Range.Text
                End If
                Return ""
            End Get
          Set(ByVal value As String)
                Dim nodoBinding As CustomXMLNode = GetNode
                If nodoBinding IsNot Nothing Then
                    nodoBinding.Text = value
                Else
                    If Me.internalDoc.Abierto Then
                        internalContentControl.Range.Text = value
                    End If
                End If
            End Set
        End Property
    
        Private ReadOnly Property GetNode As CustomXMLNode
            Get
                Dim xmlNode As CustomXMLNode = Nothing
    
                For Each part As CustomXMLPart In Me.internalDoc.InternalDocument.CustomXMLParts
                    xmlNode = part.SelectSingleNode("/Notin")
                    If xmlNode IsNot Nothing Then
                        Exit For
                    End If
                Next
                If xmlNode IsNot Nothing Then
                    Return xmlNode.SelectSingleNode("//Control[@GUID=" & Me.Id & "]")
                End If
                Return Nothing
            End Get
        End Property
    
    
    
        Public Property [readOnly] As Boolean Implements IControlContenido.readOnly
            Get
                Return internalContentControl.LockContents
            End Get
            Set(ByVal value As Boolean)
                internalContentControl.LockContents = value
            End Set
        End Property
    
        Private _valueBehavior As BehaviorsValues = BehaviorsValues.SinDefinir
    
        Public Property ValueBehavior As BehaviorsValues Implements IControlContenido.ValueBehavior
            Get
                If _valueBehavior = BehaviorsValues.SinDefinir And internalContentControl IsNot Nothing Then
                    Select Case internalContentControl.Type
                        Case WdContentControlType.wdContentControlText
                            _valueBehavior = BehaviorsValues.Texto
                        Case WdContentControlType.wdContentControlDate
                            _valueBehavior = BehaviorsValues.Fecha
                        Case WdContentControlType.wdContentControlDropdownList, WdContentControlType.wdContentControlComboBox
                            _valueBehavior = BehaviorsValues.Lista
                    End Select
                End If
                Return _valueBehavior
            End Get
            Set(ByVal value As BehaviorsValues)
                If internalContentControl IsNot Nothing Then
                    Select Case value
                        Case BehaviorsValues.Lista
                            ' Por defecto vamos a asignar este tipo de controles para los fecha
                            internalContentControl.Type = WdContentControlType.wdContentControlComboBox
                        Case BehaviorsValues.Fecha
                            internalContentControl.Type = WdContentControlType.wdContentControlDate
                        Case Else
                            internalContentControl.Type = WdContentControlType.wdContentControlText
                    End Select
                    _valueBehavior = value
                End If
            End Set
        End Property
    End Class
    

    If you see class ControlContenido in the method set_text property I try to change de text searching the customxmlnode, that it's mapped, to changed text property

    Excuse me for my english level :)

    Thanks you

    Friday, March 23, 2012 9:41 AM
  • Hi Ramon,

    The level of your english worked for me if it meant that you are changing the text in the ContentControl in this Property.Set(),

    Public Property Text As String Implements IControlContenido.Text

            Get           
    If Me.internalDoc.Abierto Then
                   
    Return internalContentControl.Range.Text
               
    End If
               
    Return ""

            End Get     
    Set(ByVal value As String)
               
    Dim nodoBinding As CustomXMLNode = GetNode
               
    If nodoBinding IsNot Nothing Then
                   
    nodoBinding.Text = value
               
    Else
                   
    If Me.internalDoc.Abierto Then
                       
    internalContentControl.Range.Text = value
                   
    End If
               
    End If

            End Set

        End Property

    Thanks for posting your code. It is very difficult to use your code to build a demonstration to exercise since your code starts with
    "Imports Formateadores".
    That isn't on my system.

    That aside, is it true that once your code runs, then you close the document when you reopen it you see the new value in the ContentControl? If so, consider trying actions that encourage Word to refresh the document. The one with the most promise is 'Document.Repaginate()' or Document.Save() saving the changes.
    Also, if you build your project as an Add-in and debug it, when you attach to Word you can set a breakpoint at the property set() and step through the code while examining the effect in the document.

    Please don't hesitate to ask for clarification of this post or let me know if I misunderstood your issue.
    Regards,
    Chris Jensen
    Senior Technical Support Lead


    Chris Jensen

    • Proposed as answer by Bruce Song Monday, April 2, 2012 8:06 AM
    Tuesday, March 27, 2012 8:38 PM
    Moderator
  • If you want to build a demostration not need Formateadores, you must open a Document with various ContentControls and you need define a  CustomXMLPart , for example:

    <ELEMENTS>

    <CONTENTCONTROL ID ="TagContentControl1">

               <TEXT></TEXT>

    </CONTENTCONTROL>

    ....

    </ELEMENTS>

    Then you must link a ContentControl element with the CustomXMLPart, Finally you change a property of the CustomXMLPart corresponding to property text of a content control, linked previous.

    if you need moreinformationon how to do I help you.

    Saludos.

    Monday, April 2, 2012 8:18 AM