Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error: Object not a collection?

532 views
Skip to first unread message

yawnmoth

unread,
Aug 1, 2005, 1:23:01 AM8/1/05
to
I'm kinda new to vbscript and don't really know why I would be getting
an error with the following script:

-------------------------

Const HKEY_CURRENT_USER = &H80000001

Dim Location, OpenSaveMRU, WshShell, oReg

OpenSaveMRU =
"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU"
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oReg = GetObject("winmgmts:root/DEFAULT:StdRegProv")

Location = "C:\Test"

oReg.EnumKey HKEY_CURRENT_USER, OpenSaveMRU, arrKeys
For Each key in arrKeys
temp = temp & key & vbCrlf
key = OpenSaveMRU & "\" & key
oReg.EnumValues HKEY_CURRENT_USER, key, arrEntries

For Each entry In arrEntries
If entry = "MRUList" Then
MRUList = WshShell.RegRead("HKEY_CURRENT_USER\" & key & "\MRUList")
Else
entry = "HKEY_CURRENT_USER\" & key & "\" & entry
value = WshShell.RegRead(entry)
If Mid(value,1,Len(Location)) = Location Then
'WshShell.RegDelete(entry)
'MsgBox(entry)
MRUList = Replace(MRUList,entry,"")
End If
End If
Next
'MsgBox(MRUList)
'WshShell.RegWrite "HKEY_CURRENT_USER\" & key & "\MRUList", MRUList,
"REG_SZ"
Next

MsgBox(temp)

-------------------------

Here's the error I get:

Line: 17
Char: 2
Error: Object not a collection
Code: 800A01C3
Source: Microsoft VBScript runtime error

Any help would be appreciated - thanks!

Torgeir Bakken (MVP)

unread,
Aug 1, 2005, 6:27:59 AM8/1/05
to
yawnmoth wrote:

> I'm kinda new to vbscript and don't really know why I would be getting
> an error with the following script:
>

> (snip code)


>
> Here's the error I get:
>
> Line: 17
> Char: 2
> Error: Object not a collection
> Code: 800A01C3
> Source: Microsoft VBScript runtime error
>
> Any help would be appreciated - thanks!
>

Hi,

You get this error because you have some keys without any values, so
your script tries to enumerate an non-existing array (collection) in
that case.

Here is a version that tests if registry keys/values returns ok with
an array (lines will wrap in the post because of line length):

'--------------------8<----------------------
Const HKEY_CURRENT_USER = &H80000001

Dim Location, OpenSaveMRU, WshShell, oReg

OpenSaveMRU =
"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU"
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oReg = GetObject("winmgmts:root/DEFAULT:StdRegProv")

Location = "C:\Test"

iRC = oReg.EnumKey(HKEY_CURRENT_USER, OpenSaveMRU, arrKeys)

If iRC = 0 And IsArray(arrKeys) Then ' registry key exists

For Each key in arrKeys

temp = temp & key & vbCrlf
key = OpenSaveMRU & "\" & key

iRC = oReg.EnumValues(HKEY_CURRENT_USER, key, arrEntries)
If iRC = 0 And IsArray(arrEntries) Then


For Each entry In arrEntries
If entry = "MRUList" Then
MRUList = WshShell.RegRead("HKEY_CURRENT_USER\" & key & "\MRUList")
Else
entry = "HKEY_CURRENT_USER\" & key & "\" & entry
value = WshShell.RegRead(entry)
If Mid(value,1,Len(Location)) = Location Then
'WshShell.RegDelete(entry)
'MsgBox(entry)
MRUList = Replace(MRUList,entry,"")
End If
End If
Next

End If


'MsgBox(MRUList)
'WshShell.RegWrite "HKEY_CURRENT_USER\" & key & "\MRUList", MRUList,"REG_SZ"
Next

End If

MsgBox(temp)
'--------------------8<----------------------

--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

yawnmoth

unread,
Aug 1, 2005, 9:51:21 AM8/1/05
to

Torgeir Bakken (MVP) wrote:
> yawnmoth wrote:
>
> <snip>

> Hi,
>
> You get this error because you have some keys without any values, so
> your script tries to enumerate an non-existing array (collection) in
> that case.
>
> Here is a version that tests if registry keys/values returns ok with
> an array (lines will wrap in the post because of line length):

That worked - thanks! Incidently, why does this work:

iRC = oReg.EnumValues(HKEY_CURRENT_USER, key, arrEntries)

when this doesn't?:

oReg.EnumValues(HKEY_CURRENT_USER, key, arrEntries)

Also, I'd like to tack on an extra value to arrEntries. In PHP, I'd do
this:

$arrEntries[] = ""

or:

$arrayEntries[count($arrEntries)] = ""

How would I do this in vBScript?

Torgeir Bakken (MVP)

unread,
Aug 1, 2005, 10:40:57 AM8/1/05
to
yawnmoth wrote:

> Torgeir Bakken (MVP) wrote:
>
>>yawnmoth wrote:
>>
>><snip>
>>Hi,
>>
>>You get this error because you have some keys without any values, so
>>your script tries to enumerate an non-existing array (collection) in
>>that case.
>>
>>Here is a version that tests if registry keys/values returns ok with
>>an array (lines will wrap in the post because of line length):
>
>
> That worked - thanks! Incidently, why does this work:
>
> iRC = oReg.EnumValues(HKEY_CURRENT_USER, key, arrEntries)
>
> when this doesn't?:
>
> oReg.EnumValues(HKEY_CURRENT_USER, key, arrEntries)

As you don't assign the return value to a variable, you will need
to use this:

oReg.EnumValues HKEY_CURRENT_USER, key, arrEntries


> Also, I'd like to tack on an extra value to arrEntries. In PHP, I'd do
> this:
>
> $arrEntries[] = ""
>
> or:
>
> $arrayEntries[count($arrEntries)] = ""
>
> How would I do this in vBScript?

I suggest you look up ReDim Preserve in the documentation.

WSH 5.6 documentation (local help file) can be downloaded
from here if you haven't got it already:
http://msdn.microsoft.com/downloads/list/webdev.asp

A Google newsgroup search for "ReDim Preserve" in the scripting
newsgroups:
http://groups.google.co.uk/groups?&as_epq=ReDim+Preserve&as_oq=&as_eq=&as_ugroup=*.scripting

Al Dunbar [MS-MVP]

unread,
Aug 1, 2005, 10:57:15 PM8/1/05
to

"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message
news:uYf4Mcql...@TK2MSFTNGP15.phx.gbl...

> yawnmoth wrote:
>
> > Torgeir Bakken (MVP) wrote:
> >
> >>yawnmoth wrote:
> >>
> >><snip>
> >>Hi,
> >>
> >>You get this error because you have some keys without any values, so
> >>your script tries to enumerate an non-existing array (collection) in
> >>that case.
> >>
> >>Here is a version that tests if registry keys/values returns ok with
> >>an array (lines will wrap in the post because of line length):
> >
> >
> > That worked - thanks! Incidently, why does this work:
> >
> > iRC = oReg.EnumValues(HKEY_CURRENT_USER, key, arrEntries)
> >
> > when this doesn't?:
> >
> > oReg.EnumValues(HKEY_CURRENT_USER, key, arrEntries)
>
> As you don't assign the return value to a variable, you will need
> to use this:
>
> oReg.EnumValues HKEY_CURRENT_USER, key, arrEntries

Or, alternately, one could do this:

call oReg.EnumValues(HKEY_CURRENT_USER, key, arrEntries)

It may be useful to note that functions/subs taking one parameter seem to
allow the form that seems to not work noted above, i.e.:

somefunc( somevalue )

Seems about equivalent to:

result = somefunc( somevalue )
or:
call somefunc( somevalue )

In actual fact, though, the parenthesis in the "somefunc (somevalue)"
statement are doing something quite different from containing a parameter
list - they are simply grouping an expression. For this reason, one should
take care to avoid confusing oneself.

/Al

0 new messages