错误:对象必需:'示例'

时间:2013-10-28 21:39:23

标签: vba vbscript

嘿我这样做直到循环将文本文件转换为字符串(“strnotapprovedData”),然后调用此函数运行命令来删除共享。我不断收到所需的错误对象:“xxxx”。我如何解决这个问题是loop语句或字符串的功能。

功能:

Function DeleteThisShare(Value)

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

End Function

循环声明:

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

Loop

字符串:

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

回复Chris Nielsen

我添加了这个,同样的问题仍然存在

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

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

Loop

1 个答案:

答案 0 :(得分:1)

作为文件系统对象的strnotapprovedData方法的结果,

ReadAll被设置为字符串。由于它是字符串而不是流,因此它不具有AtEndOfStream属性。相反,您应该将它拆分为换行符并在结果数组上循环。


对编辑的回应:

您的代码未显示正在定义或初始化objnotapprovedLines的位置。根据您的使用情况,我认为它以流形式开始,但我无从知晓。但是,在DO之后的第一行,您将其覆盖为数组。数组没有AtEndOfStream属性,因此即使没有其他内容,也肯定会导致错误。

以下是一些未经测试的代码:

' 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