简体   繁体   中英

Error: Object Required: 'example'

Hey I have this do until loop that takes a text file made into a string ("strnotapprovedData") and then calls this function that runs a command to delete the share. I keep getting the error object required: "xxxx". How do I fix this is the problem the function the loop statement or the string.

Function:

Function DeleteThisShare(Value)

   DeleteThisShare = "net share " & Share & " \DELETE"
    Set objShell = CreateObject("Wscript.Shell")
    Msgbox AddQuotes(DeleteThisShare)
    objShell.Run DeleteThisShare

End Function

Loop Statement:

Do Until strnotapprovedData.AtEndOfStream
Dim objShare : objShare = Split(strnotapprovedData,vbCrLf)
    notapprovedShares = objShare
    DeleteThisShare(notapprovedShares)

Loop

String:

Dim notapprovedList, notapprovedShares
Set notapprovedList = objFSo.OpenTextFile ("C:\Users\abro\Shares_Not_Approved.txt")
Dim strnotapprovedFile, strnotapprovedData
strnotapprovedFile = ("C:\Users\abro\Shares_Not_Approved.txt")
strnotapprovedData = objFSO.OpenTextFile(strnotapprovedFile,ForReading).ReadAll

Reply to Chris Nielsen

Well I added this and the same problem still occurs

strnotapprovedData = objFSO.OpenTextFile(strnotapprovedFile,ForReading).ReadAll

Do Until objnotapprovedLines.AtEndOfStream
    objnotapprovedLines = Split(Trim(strnotapprovedData),vbCrLf)
    DeleteThisShare(objnotapprovedLines)

Loop

strnotapprovedData is set to a string as a result of the ReadAll method of the file system object. Since it is a string and not a stream, it will not have an AtEndOfStream property. Instead, you should split it on line breaks and loop over the resulting array.


Response to edit:

Your code does not show where objnotapprovedLines is being defined or initialized. From your usage, I presume it starts life as a stream, but I have no way to know that. However, on the first line after your DO , you overwrite it to be an array. Arrays do not have an AtEndOfStream property, so that would certainly cause the error, even if nothing else has.

Here is some untested code to try:

' Define a new variable called "notApprovedLines"
' VBScript is loosely-typed, so this could be anything at this point.
Dim notApprovedLines

' Read the contents of the file into the "notApprovedLines" variable.
' This assumes that you have a variable named "strnotapprovedFile" that
' contains the path to the file, as your example code indicates.  At the
' end of this, the "notApprovedLines" variable contains the entire contents
' of the file as one giant string.
notApprovedLines = objFSO.OpenTextFile(strnotapprovedFile, ForReading).ReadAll

' Split the contents of the file into an array, so we can deal with one line at
' a time. After this, "notApprovedLines" will be an array of strings, with each
' entry representing one line of the original file.
notApprovedLines = Split(notApprovedLines, vbCrLf)

' Loop over those lines
Dim x, k, line

' This sets the upper boundary of the loop.
k = uBound(notApprovedLines)

' In VBScript, arrays start at zero.  The "x" is our counter variable. Its
' value will be incremented with each iteration of the loop, so we hit the
' entries one at a time.
For x = 0 To k

    ' Trim whitespace away from each line.  After this executes, the "line"
    ' variable will contain a single line from the file, as a string, without
    ' and leading or trailing whitespace.
    line = Trim(notApprovedLines(x))

    ' We don't want to process blank lines, if any exist.  This test lets us
    ' skip those.  Any line that contained only whitespace would also be
    ' skipped here, since it's length would be zero after trimming away the
    ' whitespace.
    If Len(line) > 0 Then

        ' This executes your function. I have not really proofed it very
        ' closely. Let's hope it works! =)
        DeleteThisShare(line)

    End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM