VBS/VBA difference

S

SteveP

In vbs, I can parse an XML file with the following code:
Set xmlDoc = CreateObject("MSXML2.DOMDocument.4.0")
If (IsObject(xmlDoc) = False) Then
alert("DOM document not created. Check MSXML version used in
createXmlDomDocument.")
End If
xmlDoc.async = False
xmlDoc.validateOnParse=False
xmlDoc.load xmlFile
If xmlDoc.parseError.errorcode = 0 Then
'Walk from the root to each of its child nodes:
treeWalk(xmlDoc)
Else
Wscript.echo "There was an error in : " & xmlFile &" Line: " &
xmlDoc.parseError.line & " Column: " & xmlDoc.parseError.linepos
End If

Function treeWalk(node)
For Each child In node.childNodes
If (child.hasChildNodes) Then
treeWalk(child)
End If
Next
End Function

When I try to do this in Excel VBA, it chokes on the line "treeWalk(xmlDoc)"
saying Object doesn't support this property or method. I tried changing the
function definition to include As all-sorts-of-things, no change. I do have
Microsoft XML 4.0 included in the VBA Project References.

I'm obviously overlooking something basic (Visually Basic, that is). Can
someone point me in the right direction?
 
T

Tom Ogilvy

You need to remove the () around your object arguments. In VBA, it violates
syntax to enclose an argument in parens unless you are passing it to a
function that returns a value or you a using the CALL method.

this worked fine for me:

Sub AAA()
xmlFile = "L:\Dataspec.xml"
Set xmlDoc = CreateObject("MSXML2.DOMDocument.4.0")
If IsObject(xmlDoc) = False Then
'alert ("DOM document not created. " & _
"Check MSXML version used in createXmlDomDocument.")
End If
xmlDoc.async = False
xmlDoc.validateOnParse = False
xmlDoc.Load xmlFile
If xmlDoc.parseError.errorcode = 0 Then
'Walk from the root to each of its child nodes:
treeWalk xmlDoc
Else
' Wscript.echo "There was an error in : " & xmlFile &" Line: " &
'xmlDoc.parseError.line & " Column: " & xmlDoc.parseError.linepos
End If
End Sub

Function treeWalk(node)
For Each child In node.childNodes

If child.hasChildNodes Then
treeWalk child
End If
Next
End Function
 
S

SteveP

Thanks, Tom! It was originally written as a function but not returning a
value, so I changed it to a Sub. That probably explains it.

Steve
 
G

gimme_this_gimme_that

Tom followed up with a nice post ....

Anyway...

Be certain that you add "Microsoft Scripting 1.0" in your reference
library - you might hit the lucky jackpot and get a definition for
that object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top