Help with a VB script.please

Joined
Aug 20, 2011
Messages
1
Reaction score
0
working on a script for school lab and this is the last one i just can't figure out where the problem is. It prints out the computer, network, and user name but it does not do the printer info. It tells me subscript out of range. Can someone please help me figure this out.

'purpose: Displays computer name, domain name and user name.
' Displays a list of all printer connections
option explicit

'output basic network information about the user and computer
dim wshNetwork
Set wshNetwork = wscript.createobject("wscript.network")

wscript.echo WshNetwork.ComputerName _
& vbcr & WshNetwork.UserDomain _
& vbcr & WshNetwork.UserName

'List the printers attached to this computer
dim PrinterCollection, printerOutput, i

set PrinterCollection = WshNetwork.enumPrinterConnections

if PrinterCollection.length = 0 then
wscript.echo "no printers"
else
printerOutput = "Printers:" & VBCR
For i = 0 to PrinterCollection.Count - 1 Step 2

next
end if


printerOutput = printerOutput & "Port " & PrinterCollection.Item(i) _
& " = " & PrinterCollection.Item(i+1) & VBCR

wscript.echo printerOutput
 
Joined
May 31, 2011
Messages
32
Reaction score
0
Hi Holcombgl

it seems that on the following line (see below), you are trying to access item(i) and item(i+1) where i is the number (n) if objects in the PrinterCollection collection; however in VBScript the index for the items in the collection start at 0 up to n - 1; in your example your indexes (i and i+1) is outside of the range 0 to i -1.

printerOutput = printerOutput & "Port " & PrinterCollection.Item(i) _
& " = " & PrinterCollection.Item(i+1) & VBCR

I managed to run your script (as it's a safe script!) without triggering any out of range errors by modifying the line above as follow:

printerOutput = printerOutput & "Port " & PrinterCollection.Item(i - 1) _
& " = " & PrinterCollection.Item(i -1) & VBCR

I hope this helps. Let me know if you have any further issues.

Sifou
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top