none
Runtime Error 6155 Namespace for this shema is already registered RRS feed

  • Question

  • Hello Community,

    I'm pretty new to VBA so please forgive me if this is trivial. I want to bind content control Elements to Custom XML without really without VSTO. I want to use plain VBA and nothing more. There is this awesome walkthrough about how to do this with VSTO. I've read it many times but it's just not the same in VBA and I needed to change some things.

    My Problem is that I can't bind an XML Shema to the document so I can Use Drop Down Lists filled from the shema.

    Here is my code:

        Private Sub Document_Open()
       
            MsgBox (ThisDocument.CustomXMLParts.Count())
            
            Call VerifySampleSchema
            Call XMLAdden
            Call BindControlsToCustomXmlPart
           
    
    
    
        End Sub
    
    Private Sub XMLAdden()
        Dim MyNamespace As String
        MyNamespace = "http://schemas.microsoft.com/vsto/samples/efsample"
        
        If (ThisDocument.CustomXMLParts.SelectByNamespace(MyNamespace).Count() = 0) Then
           
            Call ThisDocument.CustomXMLParts.Add
            ThisDocument.CustomXMLParts(ThisDocument.CustomXMLParts.Count()).Load ("O:\x_sonstiges\T2\employees.xml")
        End If
         MsgBox (ThisDocument.CustomXMLParts.Count())
    
        
    
    End Sub
    
       Private Sub AddSchema()
            Dim objSchema As Word.XMLSchemaReference
    
            objSchema = ThisDocument.XMLSchemaReferences _
            .Add("http://schemas.microsoft.com/vsto/samples/efsample", , "O:\x_sonstiges\T2\employees.xsd", True)
        End Sub
    
        Private Sub VerifySampleSchema()
            Dim objSchema As Word.XMLSchemaReference
            Dim blnSchemaAttached As Boolean
    
    
    
            For Each objSchema In ThisDocument.XMLSchemaReferences
            MsgBox (ObjShema.Location)
                If objSchema.Location <> "O:\x_sonstiges\T2\employees.xsd" Then
                    blnSchemaAttached = False
                Else
                    objSchema.Reload
                    blnSchemaAttached = True
                    Exit For
                End If
    
            Next
    
            If blnSchemaAttached = False Then
                AddSchema
            End If
    
    
    
        End Sub
    
     Private Sub BindControlsToCustomXmlPart()
    
            Dim xPathName As String
            xPathName = "ns:employees/ns:employee/ns:name"
            
            Call ThisDocument.ContentControls.Item(1).XMLMapping.SetMapping(xPathName, _
                prefix, ThisDocument.CustomXMLParts(ThisDocument.CustomXMLParts.Count()))
    
            Dim xPathTitle As String
            xPathTitle = "ns:employees/ns:employee/ns:title"
            Call ThisDocument.ContentControls.Item(2).XMLMapping.SetMapping(xPathTitle, _
                prefix, ThisDocument.CustomXMLParts(ThisDocument.CustomXMLParts.Count()))
                
           
            
        End Sub
    


    The AddShema Method throws an error "Runtime-Error 6155" "A namespace was already registered for this shema".

    Can you guys please help me to get this running?

    regards

    BG

    Some Additional Information: I'm running MS Word 2010, There ain't no shemas registered (as is proofed by the code itself because it does exit the for loop immediatly). When I run the script without attaching the shema the content elements still don't get connected with the customxml (or at least I can't see that they are filled with the values in the xml).

    <?xml version="1.0" encoding="utf-8" ?>
    <employees xmlns="http://schemas.microsoft.com/vsto/samples/efsample">
      <employee>
        <name>Karina Leal</name>
        <hireDate>1999-04-01</hireDate>
        <title>Manager</title>
      </employee>
    </employees>
    the XML Code
    <?xml version="1.0" encoding="utf-8" ?>
    <xs:schema xmlns="http://schemas.microsoft.com/vsto/samples/efsample"
        targetNamespace="http://schemas.microsoft.com/vsto/samples/efsample"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified">
      <xs:element name="employees" type="EmployeesType"></xs:element>
      <xs:complexType name="EmployeesType">
        <xs:all>
          <xs:element name="employee" type="EmployeeType"/>
        </xs:all>
      </xs:complexType>
      <xs:complexType name="EmployeeType">
        <xs:sequence>
          <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
          <xs:element name="hireDate" type="xs:date" minOccurs="1" maxOccurs="1"/>
          <xs:element name="title" type="TitleType" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
      </xs:complexType>
      <xs:simpleType name="TitleType">
        <xs:restriction base="xs:string">
          <xs:enumeration value ="Engineer"/>
          <xs:enumeration value ="Designer"/>
          <xs:enumeration value ="Manager"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:schema>
    The XML Shema


    Wednesday, October 30, 2013 3:11 PM

Answers

  • Hi MIster B-42

    Your code isn't making a lot of sense... ThisDocument is a special class that reflects the document object. It should only be used in the context of things that run in that class. Other code should not be placed in there without a special reason - and I don't perceive one, in this case.

    The following sample code works for me to:

    1) check whether the schema namespace is in question is in the Word instance's Shema Library
    2) if not, add the namespace
    3) check whether there's a schema with that namespace
    4) if not, add the schema
    5) Then a custom xml part is added
    6) The sample XML (with the same namespace!) is loaded
    7) The dropdown content control is mapped to the node

    Note that this code uses the schema and XML at http://msdn.microsoft.com/en-us/library/bb398244.aspx

    Sub MapDropdownToCXPandSchema()
        Dim sUri As String, sSchemaLocation As String, sXML As String
        Dim doc As word.Document
        
        sUri = "http://schemas.microsoft.com/vsto/samples"
        sSchemaLocation = "C:\Test\CustomXMLParts\Employees.xsd"
        sXML = "C:\\Test\CustomXMLParts\Employees.xml"
        Set doc = ActiveDocument
        GetSchema doc, sUri, sSchemaLocation
        
        Dim cxp As Office.CustomXMLPart
        Set cxp = doc.CustomXMLParts.Add()
        cxp.Load sXML
        
        Dim ccDropDown As word.ContentControl
        Dim ns As String
        Dim nd As Office.CustomXMLNode
        ns = cxp.NamespaceManager.LookupPrefix(sUri)
        Set ccDropDown = doc.Contentcontrols(3)
        Set nd = cxp.SelectSingleNode("//" & ns & ":title") '
        ccDropDown.XMLMapping.SetMappingByNode nd
    End Sub
    
    Function GetSchema(doc As word.Document, sUri As String, sSchemaLoc As String) _
        As word.XMLSchemaReference
        
        Dim schema As word.XMLSchemaReference
        Dim ns As word.XMLNamespace
        Dim bSchemaInLib As Boolean, bSchemaAttached As Boolean
    
        bSchemaInLib = False
        bSchemaAttached = False
        For Each ns In Application.XMLNamespaces
            If ns.URI = sUri Then
                bSchemaInLib = True
                Exit For
            End If
        Next
        If Not bSchemaInLib Then
            Set ns = Application.XMLNamespaces.Add(sSchemaLoc, sUri, "Test", False)
        End If
        
        For Each schema In doc.XMLSchemaReferences
            If schema.NamespaceURI = sUri Then
                bSchemaAttached = True
                Set GetSchema = schema
                Exit For
            End If
        Next
        If Not bSchemaAttached Then
            Set GetSchema = doc.XMLSchemaReferences.Add(sUri)
        End If
    End Function


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, November 5, 2013 6:35 PM
    Moderator

All replies

  • Hi MIster B-42

    Your code isn't making a lot of sense... ThisDocument is a special class that reflects the document object. It should only be used in the context of things that run in that class. Other code should not be placed in there without a special reason - and I don't perceive one, in this case.

    The following sample code works for me to:

    1) check whether the schema namespace is in question is in the Word instance's Shema Library
    2) if not, add the namespace
    3) check whether there's a schema with that namespace
    4) if not, add the schema
    5) Then a custom xml part is added
    6) The sample XML (with the same namespace!) is loaded
    7) The dropdown content control is mapped to the node

    Note that this code uses the schema and XML at http://msdn.microsoft.com/en-us/library/bb398244.aspx

    Sub MapDropdownToCXPandSchema()
        Dim sUri As String, sSchemaLocation As String, sXML As String
        Dim doc As word.Document
        
        sUri = "http://schemas.microsoft.com/vsto/samples"
        sSchemaLocation = "C:\Test\CustomXMLParts\Employees.xsd"
        sXML = "C:\\Test\CustomXMLParts\Employees.xml"
        Set doc = ActiveDocument
        GetSchema doc, sUri, sSchemaLocation
        
        Dim cxp As Office.CustomXMLPart
        Set cxp = doc.CustomXMLParts.Add()
        cxp.Load sXML
        
        Dim ccDropDown As word.ContentControl
        Dim ns As String
        Dim nd As Office.CustomXMLNode
        ns = cxp.NamespaceManager.LookupPrefix(sUri)
        Set ccDropDown = doc.Contentcontrols(3)
        Set nd = cxp.SelectSingleNode("//" & ns & ":title") '
        ccDropDown.XMLMapping.SetMappingByNode nd
    End Sub
    
    Function GetSchema(doc As word.Document, sUri As String, sSchemaLoc As String) _
        As word.XMLSchemaReference
        
        Dim schema As word.XMLSchemaReference
        Dim ns As word.XMLNamespace
        Dim bSchemaInLib As Boolean, bSchemaAttached As Boolean
    
        bSchemaInLib = False
        bSchemaAttached = False
        For Each ns In Application.XMLNamespaces
            If ns.URI = sUri Then
                bSchemaInLib = True
                Exit For
            End If
        Next
        If Not bSchemaInLib Then
            Set ns = Application.XMLNamespaces.Add(sSchemaLoc, sUri, "Test", False)
        End If
        
        For Each schema In doc.XMLSchemaReferences
            If schema.NamespaceURI = sUri Then
                bSchemaAttached = True
                Set GetSchema = schema
                Exit For
            End If
        Next
        If Not bSchemaAttached Then
            Set GetSchema = doc.XMLSchemaReferences.Add(sUri)
        End If
    End Function


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, November 5, 2013 6:35 PM
    Moderator
  • Hello Ms Meister,

    thank you for your great help. This code works a charm. I really appretiate your help.

    Wednesday, November 20, 2013 3:02 PM