computing
  • 8

Solved How To Fix ‘Run Time Error 424’ In Vb Script

  • 8

im not a familiar with programming basics.

im getting ‘Run time error 424 ‘ object required, when executing the script provided in #5

im using the script as macro in Excel, any idea what im doing wrong?

Sub XMLFind()
‘==== begin vbscript “xmlfind”
If wscript.arguments.Count < 1 Then
wscript.echo “Usage: XMLFIND C:\Users\vik\Documents\XMLTest\CD_catalog.xml targetstring”
wscript.Quit
End If
targ = wscript.arguments(1)
Set fso = CreateObject(“scripting.filesystemobject”)
Set test = fso.opentextfile(wscript.arguments(0), 1)
Z = test.readall
‘remove lcase() from foll line to observe case in search
point1 = InStr(LCase(Z), LCase(targ))
If point1 = 0 Then
wscript.echo “NOT FOUND: string: ” & targ
wscript.Quit
End If
point1 = point1 + Len(targ)
found = LTrim(Mid(Z, point1))
quote = Left(found, 1)
found = Mid(found, 2)
point2 = InStr(found, quote)
If point2 > 0 Then
found = Left(found, point2 – 1)
End If
wscript.echo Chr(34) & found & Chr(34)
‘===== end vbscript
End Sub

Script obtained from below link:
https://computing.net/answers/pr…

End result:
what i’m trying to do is to create macro or batch to extract a field name & its value from a xml file.

The XML file im using is contain 4 or 5 level nodes.

 

 

message edited by vennpura

Share

1 Answer

  1. So your biggest problem is you’re taking VBS and trying to use it as VBA. As you can clearly see, they’re 33% different.

    I don’t have easy access to Excel at the moment, but from memory, something like this?:

    Sub XMLFind()
      attributeName = "//TITLE"
      fullPath = CreateObject("Scripting.FileSystemObject") _
       .GetAbsolutePathName("Path and name to XML file here")
      Set XML = CreateObject("Msxml2.DOMDocument.6.0")
      XML.setProperty "SelectionLanguage", "XPath"
      XML.setProperty "ProhibitDTD", False
      XML.ValidateOnParse = False
      
      If Not XML.Load(fullPath) Then
        MsgBox "Failed to parse: " & fullPath _
        & vbNewLine & XML.parseError.reason
        Exit Sub
      End If
      
      For Each Node In XML.SelectNodes(attributeName)
        Selection = Node.Text
        Selection.Offsest(1, 0).Select
      Next 'node
    End Sub

    How To Ask Questions The Smart Way

    • 0