Using XML Files with Microsoft Office Project 2003
Project files may be saved in XML format by clicking Save As on the File menu, or saved to an XML file or DOM document by using Visual Basic for Applications (VBA). In Project, you can also directly open an XML file that is valid, according to the Project XML Schema. You can also open a valid XML file, DOM (Document Object Model) document, or SOAP string by using VBA.
The following are code samples for using XML with VBA in Project. To use the XML DOM, set a reference in the Visual Basic Editor of Project to Microsoft XML. It is recommended that you use Microsoft XML, v4 (normally installed in C:\WINDOWS\system32\msxml4.dll), or Microsoft XML, v5 (installed with Office 2003 in C:\Program Files\Common Files\Microsoft Shared\OFFICE11\msxml5.dll. In either case, the library namespace is MSXML2.
For recommendations on how to create a valid XML template and how to append XML data to an existing project, see Overview of XML for Project.
Open a project from an XML DOM document
The following code sample shows how to transform a DOM document into a Project XML DOM document and open it in Project. This method is useful when transforming an XML document containing project data that does not conform to the Project Data Interchange Schema.
Public Sub fileopen_xmldom(xml As String)
On Error GoTo err_fileopen_xmldom
Dim app As New MSProject.Application
Dim xmlDom, xslDom, projDom, xslFileName As String
'Load the XML string parameter into a DOM document
Set xmlDom = CreateObject("MSXML2.DOMDocument")
xmlDom.async = False
xmlDom.loadXML xml
'Specify an XSL template file name
xslFileName = "C:\project\xml\project.xsl"
'Load the XSL template into a DOM document
Set xslDom = CreateObject("MSXML2.DOMDocument")
xslDom.async = False
xslDom.Load xslFileName
'Transform the XML input into a project XML document
Set projDom = CreateObject("MSXML2.DOMDocument")
projDom.Load xmlDom.transformNode(xslDom)
'Open a project from the XML DOM document
app.FileOpen FormatID:="MSProject.XMLDOM", XMLName:=projDom
exit_fileopen_xmldom:
Exit Sub
err_fileopen_xmldom:
MsgBox "error: " & Err.Description
GoTo exit_fileopen_xmldom
End Sub
Public Sub Fileopen_xmldom_test()
Dim xmlDom, XMLfileName As String
XMLfileName = "C:\proj10\xml\xmldemo\x1.xml"
Set xmlDom = CreateObject("MSXML2.DOMdocument")
xmlDom.async = False
xmlDom.Load XMLfileName
fileopen_xmldom (xmlDom.xml)
End Sub
Open a project from an XML file
Public Sub fileopen_xmlfile()
On Error GoTo err_fileopen_xmlfile
Dim app As New MSProject.Application, xmlFile As String
'Specify a Microsoft Project XML file name
xmlFile = "C:\project\xml\someProject.xml"
'Open a project from the XML file
app.FileOpen Name:=xmlFile, FormatID:="MSProject.XML"
exit_fileopen_xmlfile:
Exit Sub
err_fileopen_xmlfile:
MsgBox "error: " & Err.Description
GoTo exit_fileopen_xmlfile
End Sub
Open a project from a string containing XML
The following code sample shows how to use Simple Object Access Protocol (SOAP) to call a Web page that returns an XML string that conforms to the Project XML Schema.
Public Sub openxml_xmlstring(xmlRequest As String)
On Error GoTo err_openxml_xmlstring
Dim app As New MSProject.Application
Dim XMLHttp As XMLHttp
Dim xmlResponse As String, URL As String
'Specify the web page to call
URL = "http://myserver/getProjectXML.asp"
'Create the SOAP client and call the page
Set XMLHttp = New XMLHttp
XMLHttp.Open "POST", URL, False
XMLHttp.send xmlRequest
xmlResponse = XMLHttp.responseXML.xml
'Open a project from the response string
app.OpenXML xmlResponse
exit_openxml_xmlstring:
Exit Sub
err_openxml_xmlstring:
MsgBox "error: " & Err.Description
GoTo exit_openxml_xmlstring
End Sub
Save a project to an XML DOM document
Public Sub filesaveas_xmldom()
On Error GoTo err_filesaveas_xmldom
Dim app As New MSProject.Application
Dim doc
'Create an XML DOM document
Set doc = CreateObject("MSXML2.DOMDocument")
'Save the project to the DOM document
app.FileSaveAs FormatID:="MSProject.XMLDOM", XMLName:=doc
exit_filesaveas_xmldom:
Exit Sub
err_filesaveas_xmldom:
MsgBox "error: " & Err.Description
GoTo exit_filesaveas_xmldom
End Sub
Save a project file as an XML file
Public Sub filesaveas_xmlfile()
On Error GoTo err_filesaveas_xmlfile
Dim app As New MSProject.Application, xmlFile As String
'Specify a Microsoft Project XML file name
xmlFile = "C:\path to location\fileName.xml"
'Open a project from the XML file
app.FileSaveAs Name:=xmlFile, FormatID:="MSProject.XML"
exit_filesaveas_xmlfile:
Exit Sub
err_filesaveas_xmlfile:
MsgBox "error: " & Err.Description
GoTo exit_filesaveas_xmlfile
End Sub