奇怪的VBScript错误:需要对象:'objFolder'(Strange VBScript error: Object required: 'objFolder�

编程入门 行业动态 更新时间:2024-10-25 00:23:53
奇怪的VBScript错误:需要对象:'objFolder'(Strange VBScript error: Object required: 'objFolder')

我已经在互联网上进行了广泛的搜索,但仍无法找到解决方案。 有趣的是我的代码以前工作过。 我正在使用带有VBScript代码的html页面,使用IE 9打开。

我的代码如下:

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

该文件夹存在并且检索没有问题。 不知道发生了什么。 有人能解释一下这个问题吗?

I have done extensive search on internet but still was not able to find the solution. The interesting thing is that my code worked before. I am using html page with VBScript code, opened using IE 9.

My code is below:

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

I am showing the error in line 30: Object required 'objFolder'

I added a message box in line 31 and it was reached, outputting message box with a number of subfolders in a give folder. If the problem was actually in line 30, it would never reach line 31. If I completely remove line 31 (the one with a message box), I still get the same error in line 30.

My function above is called the following way:

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

The folder exists and is retrieved without a problem. Not sure what is happening. Can someone shed some light on the issue?

最满意答案

下一个脚本按照我之前的评论中的建议收集/回应一些调试信息

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变量来代替原始的 ProcessFolder调用:

==> 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

Next script collects/echoes some debugging info as advised in 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

Debugging scenario:

==> 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

Output shows that leafs in folder tree are processed twice so the script above requires more thinking and debugging: note that strResult variable is updated in place of original ProcessFolder call:

==> 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

更多推荐

本文发布于:2023-08-05 04:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   对象   错误   VBScript   objFolder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!