Search code examples
xmlvbscriptmsxml

Getting an MSXML 80070057 "incorrect parameter" error in VBS script


Everything in this little VBS script works right up until I reach the last line of code indicated below. Upon reaching that line the script throws the error "incorrect parameter 80070057.

After spending a great deal of time googling it turns out that the error code means roughly the same thing, incorrect parameter.

'section 15
Set lots = xmlDoc3.selectNodes("ArrayOfLot/Lot")

For Each lot in lots
    Dim nl: Set nl = lot.cloneNode(true)
    xmlDoc4.documentElement.appendChild nl
Next

xmlDoc4.save xmlDoc4.url 'this code works

'**************************************************************************
'section 16 
Set lots = xmlDoc5.selectNodes("ArrayOfLot/Lot")

For Each lot in lots
    Dim nll: Set nll = lot.cloneNode(true)
    xmlDoc6.documentElement.appendChild nll
Next

xmlDoc6.save xmlDoc6.url 'this does not work - error thrown

It is really frustrating because the .save works just 8 lines above that. Does anyone have any insight to what my problem may be and how I can solve it?

Based on the answer below I am posting the code where all of the document information gets declared:

Dim xmlFilePath6: xmlFilePath6 = "section16.xml"
Dim xmlDoc6: set xmlDoc6 = CreateObject("MSXML2.DomDocument")
xmlDoc6.async = "false"
xmlDoc6.load xmlFilePath6

This actually gets done for 6 different documents, subbing the 6 for one of the other digits 1-8. I'm still puzzled because section16.xml exists and no error is thrown on load.


Solution

  • UPDATE: I adjusted my code so that xmlDoc6 is no longer associated with a file and I have reproduced your error:

    demo.vbs(67, 1) msxml3.dll: The parameter is incorrect.
    

    To reproduce your error, instead of this:

    xmlDoc6.load "xmlDoc6.xml"
    

    ...I initialized xmlDoc6 this way:

    xmlDoc6.loadXML xmlText
    

    Hunch: Make sure that xmlDoc6 is associated with a file. If it is not, the value of xmlDoc6.url will be the empty string.


    ORIGINAL: I have written a complete program to illustrate your use case. However, I am not getting the incorrect parameter error that you report when attempting xmlDoc6.save xmlDoc6.url. But perhaps something in my program will stand out that will lead you to a solution.

    For anyone interested, to run this program, copy the text to a file named say, demo.vbs, and then run it via:

    cscript demo.vbs
    

    The output to the console should look something like this:

    Creating XML documents 3 through 6.
    Saving file:///c:/Users/DavidRR/temp/xmlDoc4.xml
    Saving file:///c:/Users/DavidRR/temp/xmlDoc6.xml
    Done.
    

    ' demo.vbs - Use MSXML to edit and save multiple XML files.
    
    Option Explicit
    
    Dim xmlText : xmlText = "" _
        & "<?xml version='1.0' encoding='utf-8'?>" _
        & "<ArrayOfLot>" _
        & "  <Lot>" _
        & "    Lot One" _
        & "  </Lot>" _
        & "  <Lot>" _
        & "    Lot Two" _
        & "  </Lot>" _
        & "  <Lot>" _
        & "    Lot Three" _
        & "  </Lot>" _
        & "</ArrayOfLot>" _
        & ""
    
    WScript.Echo "Creating XML documents 3 through 6."
    
    Dim  xmlDoc3 : Set xmlDoc3 = CreateObject("Msxml2.DOMDocument")
    Dim  xmlDoc4 : Set xmlDoc4 = CreateObject("Msxml2.DOMDocument")
    Dim  xmlDoc5 : Set xmlDoc5 = CreateObject("Msxml2.DOMDocument")
    Dim  xmlDoc6 : Set xmlDoc6 = CreateObject("Msxml2.DOMDocument")
    
    xmlDoc3.loadXML xmlText
    If xmlDoc3.parseError.errorCode <> 0 Then
        WScript.Echo "Couldn't load xmlDoc3: " & xmlDoc3.parseError.reason
        WScript.Quit(1)
    Else
        ' WScript.Echo "Loaded XML [" & xmlDoc3.documentElement.xml & "]"
    End If
    
    ' WScript.Echo xmlDoc3.Xml
    xmlDoc3.save "xmlDoc4.xml"
    xmlDoc3.save "xmlDoc5.xml"
    xmlDoc3.save "xmlDoc6.xml"
    
    xmlDoc4.load "xmlDoc4.xml"
    xmlDoc5.load "xmlDoc5.xml"
    xmlDoc6.load "xmlDoc6.xml"
    ' xmlDoc6.loadXML xmlText
    ' WScript.Echo "xmlDoc6.url = [" & xmlDoc6.url & "]"
    
    ' section 15
    Set lots = xmlDoc3.selectNodes("ArrayOfLot/Lot")
    ' WScript.Echo lots.length
    
    Dim lot, lots
    For Each lot In lots
        Dim nl: Set nl = lot.cloneNode(True)
        xmlDoc4.documentElement.appendChild nl
    Next
    
    WScript.Echo "Saving " & xmlDoc4.url
    xmlDoc4.save xmlDoc4.url 'this code works
    
    '**************************************************************************
    ' section 16
    Set lots = xmlDoc5.selectNodes("ArrayOfLot/Lot")
    
    For Each lot in lots
        Dim nll: Set nll = lot.cloneNode(true)
        xmlDoc6.documentElement.appendChild nll
    Next
    
    WScript.Echo "Saving " & xmlDoc6.url
    xmlDoc6.save xmlDoc6.url ' reportedly does not work...but it works here.
    
    Set xmlDoc6 = Nothing
    Set xmlDoc5 = Nothing
    Set xmlDoc4 = Nothing
    Set xmlDoc3 = Nothing
    
    WScript.Echo "Done."
    
    ' End