2016-06-27 28 views
1

我已經做了互聯網上的廣泛搜索,但仍然沒能找到解決方案。有趣的是,我的代碼之前工作。我使用與VBScript代碼html頁面,打開使用IE 9.奇怪的VBScript錯誤「objFolder」

我的代碼是下面:

29: Function TraverseDirectory(objFolder, searchTerm, outFile) 
30: if objFolder.SubFolders.Count > 0 then <-- ERROR shown in this line: Object required: 'objFolder' 
31:  MsgBox objFolder.SubFolders.Count <-- This message is shown without an issue 
32:  Set fc = objFolder.SubFolders 
33:   For Each f1 in fc 
34:   ProcessFolder f1, searchTerm, outFile 
35:    TraverseDirectory f1, searchTerm, outFile 
36:   Next 
37: else 
38:  ProcessFolder objFolder, searchTerm, outFile 
39: end if 
40: End Function 

我顯示該錯誤在管線30:所需的對象 'objFolder'

我加第31行的消息框已到達,輸出帶有許多子文件夾的消息框。如果問題在第30行實際上是,它永遠不會達到31行。如果我徹底清除線31(其中帶有一個消息框),我還是得到線相同的錯誤30

上面我的函數被調用採用以下方式:

Set objFolder = objFSO.GetFolder("C:\Test") 
TraverseDirectory objFolder, str, outFile 

該文件夾存在並且檢索時沒有問題。不知道發生了什麼事。有人可以解釋這個問題嗎?

+0

我不能重現該問題,但*威力*是一個範圍問題與'objFolder'變量。你可以通過改變'objFolder'來測試這個功能,例如'objFolderInsideFunction'嗎? – langstrom

+1

第29行到第30行之間和第一個「TraverseDirectory」之前的'MSGBOX VarType(objFolder)&vbTab&TypeName(objFolder)'的輸出是什麼? – JosefZ

+0

輸出是「8文件夾」後跟「9無」 – ElenaDBA

回答

3

下一個腳本收集/呼應一些調試信息如建議在my previous comment

option explicit 
'On Error Resume Next 
On Error GoTo 0 
Dim strResult: strResult = Wscript.ScriptName 
Dim objfso, str, outfile, objFolder 
set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFolder = objFSO.GetFolder("D:\TestC") 
'Set objFolder = objFSO.GetFolder("C:\attachments") 'an empty folder for debugging' 
Wscript.Echo "start" & vbTab _ 
    & VarType(objFolder) & " " & TypeName(objFolder) & vbTab & objFolder 
TraverseDirectory objFolder, str, outFile 
Wscript.Echo strResult 
Wscript.Quit 

Function TraverseDirectory(objFolder, searchTerm, outFile) 
    Dim fc, f1, aux 
    Wscript.Echo "debug" & vbTab _ 
     & VarType(objFolder) & " " & TypeName(objFolder) & vbTab & objFolder 
    aux = objFolder.SubFolders.Count 
    if aux > 0 then '<-- ERROR shown in this line: Object required: 'objFolder' 
     'MsgBox objFolder.SubFolders.Count ' <-- This message is shown without an issue 
     Set fc = objFolder.SubFolders 
      For Each f1 in fc 
       strResult = strResult & vbNewLine & Cstr(aux) _ 
        & vbTab & VarType(f1) & " " & TypeName(f1) & vbTab & f1 
       'ProcessFolder f1, searchTerm, outFile 
       TraverseDirectory f1, searchTerm, outFile 
      Next 
    else 
     'ProcessFolder objFolder, searchTerm, outFile 
     strResult = strResult & vbNewLine & Cstr(aux) & vbTab _ 
      & VarType(objFolder) & " " & TypeName(objFolder) & vbTab & objFolder 
    end if 
End Function 

調試方案

==> tree "D:\TestC" 
Folder PATH listing for volume DataDisk 
Volume serial number is … … … 
D:\TESTC 
├───bubu 
│ └───foobar 
├───kuku 
├───New Folder 12 
└───New Folder 21 
    └───New folder XX 

輸出顯示,在文件夾樹葉子所以腳本被處理兩次以上需要更多的思考和調試:請注意,strResult變量更新代替o riginalProcessFolder電話:

==> cscript D:\VB_scripts\SO\38056552.vbs 
start 8 Folder  D:\testC 
debug 8 Folder  D:\testC 
debug 8 Folder  D:\testC\bubu 
debug 8 Folder  D:\testC\bubu\foobar 
debug 8 Folder  D:\testC\kuku 
debug 8 Folder  D:\testC\New Folder 12 
debug 8 Folder  D:\testC\New Folder 21 
debug 8 Folder  D:\testC\New Folder 21\New folder XX 
38056552.vbs 
4  8 Folder  D:\testC\bubu 
1  8 Folder  D:\testC\bubu\foobar 
0  8 Folder  D:\testC\bubu\foobar 
4  8 Folder  D:\testC\kuku 
0  8 Folder  D:\testC\kuku 
4  8 Folder  D:\testC\New Folder 12 
0  8 Folder  D:\testC\New Folder 12 
4  8 Folder  D:\testC\New Folder 21 
1  8 Folder  D:\testC\New Folder 21\New folder XX 
0  8 Folder  D:\testC\New Folder 21\New folder XX