title: Remove a node from XML file in MS Project VBA
body: <p>I’d like to remove a node from my xml file using <strong>VBA in MS Project 2007</strong>.</p>
  
  <p>Should be so easy but I can’t get it running.</p>
  
  <p><strong>Here is my XML</strong></p>
  
  <pre><code>&lt;config id="config" ConfigSaveDate="2011-03-31 21:32:55" ConfigSchemaVersion="1.02"&gt;
      &lt;Custom&gt; 
      &lt;/Custom&gt;
      &lt;Program&gt;
        &lt;DateFormat&gt;yyyy-mm-dd hh:mm:ss&lt;/DateFormat&gt; 
      &lt;/Program&gt;
      &lt;ProjectFile ProjectFileName="projectfile1.mpp"&gt;
        &lt;RevisionNumber&gt;201&lt;/RevisionNumber&gt; 
        &lt;FileName&gt;projectfile1.mpp&lt;/FileName&gt; 
        &lt;LastSaveDate&gt;2011-03-23 16:45:19&lt;/LastSaveDate&gt; 
      &lt;/ProjectFile&gt;
      &lt;ProjectFile ProjectFileName="projectfile2bedeleted.mpp"&gt;
        &lt;RevisionNumber&gt;115&lt;/RevisionNumber&gt; 
        &lt;FileName&gt;projectfile2bedeleted.mpp&lt;/FileName&gt; 
        &lt;LastSaveDate&gt;2011-03-31 21:12:55&lt;/LastSaveDate&gt; 
      &lt;/ProjectFile&gt;
      &lt;ProjectFile ProjectFileName="projectfile2.mpp"&gt;
        &lt;RevisionNumber&gt;315&lt;/RevisionNumber&gt; 
        &lt;FileName&gt;projectfile2.mpp&lt;/FileName&gt; 
        &lt;LastSaveDate&gt;2011-03-31 21:32:55&lt;/LastSaveDate&gt; 
      &lt;/ProjectFile&gt;
  &lt;/config&gt;
  </code></pre>
  
  <p><strong>Here is my VBA code</strong></p>
  
  <pre><code>Function configProjListDelete(configPath As String, ProjFiles As Variant) As Integer
  
    ' This function shall delete &lt;ProjectFile&gt; tags from the config.xml
    ' and shall delete coresponding project xml files from HD
    ' It shall return number of deleted files
  
    ' configPath is the  path to the xml folder
    ' ProjFiles is an array of file names of to be deleted files in above mentioned folder
  
    Dim xml As MSXML2.DOMDocument
    Dim RootElem As MSXML2.IXMLDOMElement
    'Dim cxp1 As CustomXMLPart
    Dim delNode As MSXML2.IXMLDOMNode ' XmlNode 'MSXML2.IXMLDOMElement
    Dim fSuccess As Boolean
    Dim ProjectFileList As MSXML2.IXMLDOMElement
    Dim fn As Variant 'file name in loop
    Dim i As Integer
    Dim delCnt As Integer
  
    If Not FileExists(configPath) Then
      ' given configFile doesn't exist return nothing
      Debug.Print "  iven config file doesn't exist. File: " &amp; configPath
      GoTo ExitconfigProjListDelete
    End If
  
    'TODO: Catch empty ProjectFiles
  
    ' Initialize variables
    Set xml = New MSXML2.DOMDocument
  
    On Error GoTo HandleErr
    ' Load the  XML from disk, without validating it.
    ' Wait for the load to finish before proceeding.
    xml.async = False
    xml.validateOnParse = False
    fSuccess = xml.Load(configPath)
    On Error GoTo 0
    ' If anything went wrong, quit now.
    If Not fSuccess Then
      GoTo ExitconfigProjListDelete
    End If
  
    Set RootElem = xml.DocumentElement
  
    Debug.Print "- " &amp; xml.getElementsByTagName("ProjectFile").Length &amp; " ProjectFiles in config."
  
    i = 0
    delCnt = 0
    ' Loop through all ProjectFiles
    For Each ProjectFileList In xml.getElementsByTagName("ProjectFile")
  
      ' check if each project file name is one of the files to be deleted
      For Each fn In ProjFiles
        If fn = ProjectFileList.getElementsByTagName("FileName").NextNode.nodeTypedValue Then
          Debug.Print fn &amp; " shall be deleted"
  
          ' remove it from the document
  
          ' here I'm struggeling!
          '#################################################
          ' How to delete the node &lt;ProjectFile&gt; and its childNodes?
          Set delNode = ProjectFileList.ParentNode
          xml.DocumentElement.RemoveChild (ProjectFileList) ' Error: 438 rough translation: "Object doesn't support this methode"
  
          ' This is all I've tried, but nothing works
          '===========================================
          'RootElem.RemoveChild (delNode)
          'xml.RemoveChild (delNode)
          'RootElem.RemoveChild (ProjectFileList.SelectSingleNode("ProjectFile"))
          'ProjectFileList.ParentNode.RemoveChild (ProjectFileList.ChildNodes(0))
  
          'Set objParent = datenode.ParentNode
          'xmldoc.DocumentElement.RemoveChild (objParent)
  
          'Set ProjectFileList = Empty
  
  
          delCnt = delCnt + 1
        End If
      Next fn
  
      i = i + 1
    Next ProjectFileList
  
    ' Save XML File
    If checkAppPath("Trying to update config file.") Then
      xml.Save CustomProperty("XMTMLMonitoring_AppPath") &amp; "\" &amp; m2w_config("SubFolder") &amp; "\" &amp; m2w_config("SubFolderData") &amp; "\" &amp; m2w_config("XMLConfigFileName")
      Debug.Print "  - Config has been updated and saved."
    Else
      MsgBox "Config data not exported to web." &amp; Chr(10) &amp; "Folder: '" &amp; CustomProperty("XMTMLMonitoring_AppPath") &amp; "\" &amp; m2w_config("SubFolder") &amp; "\" &amp; m2w_config("SubFolderData") &amp; Chr(10) &amp; "doesn't exist. ", vbOKOnly, HEADLINE
    End If
  
    Set xml = Nothing
  
    configProjListDelete = delCnt
  
  ExitconfigProjListDelete:
  Exit Function
  
  HandleErr:
    Debug.Print "XML File reading error " &amp; Err.Number &amp; ": " &amp; Err.DESCRIPTION
    MsgBox "Error " &amp; Err.Number &amp; ": " &amp; Err.DESCRIPTION
    On Error GoTo 0
  
  
  End Function
  </code></pre>
  
  <p>I’d be glad to get some help!</p>