Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Object required" – path string unrecognized in windows scripting file

I’m writing a wsf that opens a csv, reads the contents and prints them like so:

<job id="Test_Script">
   <script language="VBScript">

    Dim objFSO, objExcel, objConn, strOutPath, objOutFile, strSourcePath, strData    

    Set strSourcePath = "C:\test\ERACSV\"
    Set strSourceFile = "Payments.csv"
    Set objFSO        = CreateObject("Scripting.FileSystemObject")   
    Set objInFile     = objFSO.OpenTextFile(strSourcePath & strSourceFile, 1, False)
    Set strData       = objInFile.ReadAll
    objInFile.Close

    WScript.Echo strData
   </script>
</job>

I keep getting this weird error:

Script:  C:\Users\myuser\openCSV.wsf
Line:    4
Char:    4
Error:   Object Required '[string: "C:\test\ERACSV"]'
Code:    800A01A8
Source:  Microsoft VBScript runtime error

It seems weird because why is the object it requiring a string I’ve already defined?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  • Set in VBScript is only for "object-types" (COM Objects, etc), but String values are not "objects" (as far as VBScript is concerned), so don’t use Set.

    • You can either remove the Set keyword but otherwise keep the assignment statement as-is, or altenatively use the Let keyword instead of Set: Let is for non-object values (including Strings, but also Integers and Doubles).
  • In VBScript, when calling a Function (or COM Method) that returns a value you need to specify parentheses, you only omit the parentheses when calling a Sub (i.e. a void function or method) (unless you’re using the Call statement), in this case TextStream.ReadAll() returns a String value.

  • Use the named-constant ForReading instead of the magic-number literal 1.

  • You also should add Option Explicit at the first-line of your script, otherwise VBScript won’t care about variable declarations (indeed: objOutFile is declared, but objInFile is what’s actually used….)

<job id="Test_Script">
   <script language="VBScript">

    Option Explicit

    Dim objFSO, strSourcePath, strSourceFile, objInFile, strData    

    Let strSourcePath = "C:\test\ERACSV\"
    Let strSourceFile = "Payments.csv"
    Set objFSO        = CreateObject("Scripting.FileSystemObject")   
    Set objInFile     = objFSO.OpenTextFile(strSourcePath & strSourceFile, ForReading, False)
    Let strData       = objInFile.ReadAll()
    objInFile.Close

    WScript.Echo strData
   </script>
</job>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading