Troubleshooting the “Object Is Required” Error

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I am having a problem with my script. It keeps coming back and saying that an object is required. Why does it do that?

- KA

SpacerHey, Scripting Guy! Answer

Hi KA,

"Object required" is very generic. It means that (wait for it) an object is required. It happens when you try to use a method or query a property on an object, but the object has not been created.

There are two ways objects get created in BLOCKED SCRIPT

When you use CreateObject or GetObject, an object is returned.

When you query a property or call a method, sometimes an object is returned.

Perhaps an illustration will make it a bit easier to understand. In the following script, we create an instance of the filesystemobject, use the GetFolder method to return a folder object representing the C:\fso folder, and we use for each next to walk through the files in the folder:

BadScriptDoesNotWork.vbs

Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfso.GetFolder("c:\fso")
For Each file In objFlder.Files
 WScript.Echo file.name
Next

The problem is that it does not work. We get the following error:

Untitled3.vbs(17, 1) Microsoft VBScript runtime error: Object required: 'objFlder'

When I used to travel around the world all the time—literally all the time—teaching VBScript, WMI, and recently Windows PowerShell, the most difficult task I had was getting the students to stop to read the error messages. The error just above actually tells us a lot of information. Note that it tells us the error occurs on line 17. This is seen here:

Untitled3.vbs(17, 1)

It also tells us that it is a runtime error. A runtime error is one that occurs while the script is running:

Microsoft VBScript runtime error:

What kind of runtime error is it? Well, that is what the next part tells us. It says that an object is required. While that is a bit cryptic, the error helps us out. Note that it shows objFlder here:

Object required: 'objFlder'

When we examine our script, we can see that the variable we created to hold the folder object was in fact objFolder not objFlder:

Set objFolder = objfso.GetFolder("c:\fso")

So all we need to do is change the appropriate line on our script:

GoodScriptThisOneWorks.vbs

Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfso.GetFolder("c:\fso")
For Each file In objFolder.Files
 WScript.Echo file.name
Next

How can we help to avoid this problem? In the case of the bad script, the problem was a typo. All you need to do is use Option Explicit and declare all your variables. In this way, a problem with a misspelled variable name is easy to detect. Keep in mind that Option Explicit must be the first noncommented line in the script. To save space, you can separate your variable names with commas. This is seen here:

Option Explicit
Dim fso, objfso, objFolder, file

That is cool, but it leaves us with another problem. What if the folder does not exist? When we try to use objFolder.Files we will once again get an object required. This is because if the folder is not there, we cannot create a folder object for it. To solve that problem, we can use the folderexists method. We also need to make sure we end the script at that point, or we would continue to the next line and generate the error. This is seen here:

BetterScriptThatWorks.vbs

Option Explicit
Dim fso, objfso, objFolder, file 
fso = "C:\fso55"
Set objfso = CreateObject("Scripting.FileSystemObject")
if objfso.FolderExists(fso) Then
 Set objFolder = objfso.GetFolder(fso)
Else
 WScript.Echo "Dude, I cannot find the " & fso & " folder"
 Wscript.Quit
End If
For Each file In objFolder.Files
 WScript.Echo file.name
Next

In your case, if your script is looking for the active sheet but is not finding it, you would get the "Object required" error. You need to look at your code, and see exactly which line causes the error (the error message tells you), and then look back a few lines until you find where the error was supposed to have been created. This may be the source of your problem.

 

Can I Read the Serial Numbers of Printers or Scanners?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there any way I can read the serial number of a printer or a scanner?

- HH

SpacerHey, Scripting Guy! Answer

Hi HH,

Sure, go over and read it. Make sure you take a piece of paper, pencil, and maybe a flash light to aid in seeing behind the thing. Unfortunately, the Win32_Printer WMI class does not expose the serial number and there is no Win32_Scanner class.

How Do I Pull the Description Attribute from User Objects in Active Directory Domain Services?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! Can you possibly help me? I need to be able to pull the description attribute from user objects in Active Directory Domain Services (AD DS). How can I do this?

- MC

SpacerHey, Scripting Guy! Answer

Hi MC,

Because we are in quick-answer mode: I can do this in three lines of code with Windows PowerShell (did that sound too much like Name That Tune?). I do not even need to "write a script" if I do not want to. We talked about these techniques all this week in our AD DS series. By the way, this article talks about getting the description by using VBScript. This article talks about getting all the users in AD DS. If you combine the two, you will have the functionality from these three lines of code:

PS C:\> $adsi = New-Object DirectoryServices.DirectorySearcher("LDAP://dsroot")
PS C:\> $adsi.Filter = "(objectcategory=user)"
PS C:\> $adsi.FindAll() | ForEach-Object { ([adsi]$_.path).description 
Top of pageTop of page

How Do I Use DSOFile.dll to Read Microsoft Office Metadata?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I am trying to use DSOFile.dll to read Microsoft Office metadata as suggested in this “Hey, Scripting Guy!” article, but it is not working. It keeps giving me bogus errors. Why?

- FM

SpacerHey, Scripting Guy! Answer

Hi FM,

I have news for you. If you are getting errors, it is because the script is not working for you. Of course, this means the errors are not bogus. A couple of things could be happening here. First, make sure you have the latest version of DSOFile.dll. Second, you may want to refer to KB 224351 for more information about using DSOFile.dll.

One thing to keep in mind is that older versions of DSOFile.dll had different property names than the newer one does, so you may need to modify older scripts. Also, if you are trying to read Microsoft Office 2007 files, you will need to download and install the Microsoft Office Compatibility Pack. Keep in mind there is new version of the Microsoft Office Compatibility Pack for SP1. You can get it from the same page.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys