Bonjour,

je suis entrain de développer une macro vba pour générer un fichier xml a partir d'une feuille de donnée excel. Voici une partie du code fonctionnelle:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
Dim objDom As DOMDocument
     Dim objParent As IXMLDOMElement
     Dim objChild As IXMLDOMElement
     Dim oNode As IXMLDOMNode
     Dim Cmt As IXMLDOMComment
     Dim rowCount As String
     Dim columnCount As String
     Dim value As String
     Dim temp As String
 
    'Configuration
    locationZip = Sheets("Configuration").Range("B3").value
    fileNamexml = "test.xml"
 
 
    'Initialization
    rowCount = Application.WorksheetFunction.CountA(Range("A1:A65536"))
    columnCount = Range("A1:A255").End(xlToRight).Column
    Set objDom = New DOMDocument
 
     'properties xml file
    Set oNode = objDom.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
    Set oNode = objDom.InsertBefore(oNode, objDom.ChildNodes.Item(0))
 
    'Creates root element core:cdm
    Set objParent = objDom.createElement("cdm:CDM")
    Call memberElementAttribute(objParent, objDom, "xmlns:cdm", "urn:be:fgov:ehealth:cobrha:core")
    Call memberElementAttribute(objParent, objDom, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
    Call memberElementAttribute(objParent, objDom, "xsi:schemaLocation", "urn:be:fgov:ehealth:cobrha:core test.xsd")
    objDom.appendChild objParent
     i = 2
      Do While i <= Application.WorksheetFunction.CountA(Range("A1:A65536"))
    'individual 
    Call memberElement(objParent, objChild, objDom, "Individual", "")
    Call memberElementAttribute(objChild, objDom, "OperationID", Trim(Str(i - 1)))
    Call memberElementAttribute(objChild, objDom, "StartDate", "2009-01-01")
    Call memberElementAttribute(objChild, objDom, "InvalidElement", "0")
       i = i + 1
        Loop
 
    objDom.Save ("C:\Temp\Testcase\" & fileNamexml)
 
End Sub
Procédures apellée:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sub memberElement(objRoot As IXMLDOMElement, objMember As IXMLDOMElement, objDom As DOMDocument, memberName As String, value As String)
    Set objMember = objDom.createElement(memberName)
    objRoot.appendChild objMember
    objMember.Text = value
 
End Sub
 
 
Sub memberElementAttribute(objMember As IXMLDOMElement, objDom As DOMDocument, ElemattributeName As String, value As String)
     Dim objMemberAttr As IXMLDOMAttribute
     Set objMemberAttr = objDom.createAttribute(ElemattributeName)
    objMemberAttr.NodeValue = value
    objMember.setAttributeNode objMemberAttr
End Sub
Dans le code, je voudrais insérer un nouveau memberElement
où objChild qui réprésente le dernier élement fils créé devienne l'élément père.

Ex:
ObjParent=ObjChild
Call memberElement(objParent, objChild, objDom, "Individual", "")

Je reçois une erreur quand j'assigne l'objChild à l'objParent. Si quelqu'un peut m'aider afin de résoudre ce problème ou bien proposer une meilleure solution?