Obtaining return value form MSXML Save method in VBA (1 Viewer)

mdlueck

Sr. Application Developer
Local time
Today, 03:29
Joined
Jun 23, 2011
Messages
2,631
Greetings,

I am working with the MS XML objects and having difficulty obtaining a return code from the Save method. If the save fail (for testing I set the read-only attribute bit to force the save to fail), immediately the code jumps to my error handler.

Per the documentation:

save Method (DOMDocument)
http://msdn.microsoft.com/en-us/library/windows/desktop/ms753769(v=vs.85).aspx

there appears to be a C/C++ Return Value. Why does the VBA implementation not return a value? Sample code as follows:

Code:
  Dim objXMLDoc As Object
  Dim objXMLNodes As Object
  Dim vntRC As Variant

  Set objXMLDoc = CreateObject("Msxml2.DOMDocument.6.0")

  If objXMLDoc.Load("C:\Documents and Settings\c_mlueck\Desktop\6041905.xml") Then
    Set objXMLNodes = objXMLDoc.childNodes
    Call DisplayXMLNode(objXMLNodes, 0)
  End If

[COLOR=Red][B]  vntRC = objXMLDoc.Save("C:\Documents and Settings\c_mlueck\Desktop\6041905save.xml")[/B][/COLOR]
  Debug.Print "vntRC=>" & vntRC & "<"

Exit_btnTestXML_Click:
  Set objXMLNodes = Nothing
  Set objXMLDoc = Nothing
 

Rx_

Nothing In Moderation
Local time
Today, 01:29
Joined
Oct 22, 2009
Messages
2,803
Found this interesting because of a project I will start on next month. Had this to modify from MS Project to MS Access later. It looks as if an error trap is what was recommended.
It might be nice to see a validation rather than just the absence of an error.

Code:
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

Found this article on MSDN about ADO. My preference is to use DAO, but for my future XML project, it was of interest. - this is all the code they provide :
Code:
Dim xDOM As New MSXML.DOMDocument
Dim rsXML As New ADODB.Recordset
Dim sSQL As String, sConn As String
 
sSQL = "SELECT customerid, companyname, contactname FROM customers"
sConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb"
rsXML.Open sSQL, sConn
rsXML.Save xDOM, adPersistXML   'Save Recordset directly into a DOM tree.
...
Thought it was interesting that a ADO recordset could communicate directly to a DOMDocument.
Sorry I couldn't be of more help.
 

mdlueck

Sr. Application Developer
Local time
Today, 03:29
Joined
Jun 23, 2011
Messages
2,631
It looks as if an error trap is what was recommended.

Thank you for confirming the ugly work-around. Setting a custom error handler for one LOC, then having to put the error handler back to the normal way.
 

Rx_

Nothing In Moderation
Local time
Today, 01:29
Joined
Oct 22, 2009
Messages
2,803
Your absolutely right. The C++ return value shows there is an iUnknown interface layer to the object. They could just build the interface in. They may have and didn't expose it for the VBA interface.
A wrapper to a Windows DLL would be better than our community having to build a custom C++ applicaiton that would need to be referenced. In my case, it would just be on the Citrix server. But others would have to distribute it to each client desktop.

Since I wil have to deal with this later myself, the question was posted on the MSDN VBA forum.
http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/53b1ebf5-6ac1-486a-bd04-a23b7472b9c5
Please feel free to add to it or watch it. Will let you know if anything useful comes back.
Excellent question and point - hope you get a good answer because there are no good answers to be found eslewhere.

For others who have MSAccess to XML projects:
http://www.access-programmers.co.uk/forums/showthread.php?t=242332
This web page describes the various XML standards and has sample School Menu. This code example harvest a website XML and saves as to a local drive using MS Access 2010. However, the error code is very generic. While writing it, I tried to save it to a protected folder. The error code was very generic and caused me to spend needless time to understand it.

Update: The social.msdn site validated it only provides an error message. They don't make Microsoft lanugage object model programmers like they use to...
 
Last edited:

mdlueck

Sr. Application Developer
Local time
Today, 03:29
Joined
Jun 23, 2011
Messages
2,631
Yes I saw the moderator's response.

In my mind, unfortunately inconsistently standardized that LoadXML supports a return code, while Save requires trapping errors it raises. :confused:
 

Users who are viewing this thread

Top Bottom