0

I am attempting to extract the printers from a users machine and then output to a text file but when I run the test I get a invalid procedure call or argument for this specific line of code.

Set objOutputFile = objFSO.OpenTextFile(outFile, ForAppending, True)

I have attempted to change OpenTextFileto CreateTextFile but I need the lines to appended to file as it will be running as a log on script.

I have done some research and used the Microsoft developer articles to help me debug the issue in the code but I don't have much experience in Visual Basic.

I have added the entire script to give context to the what is going on.

dim objComputerName, ObjNetwork , strText , objfile, StrComputer
dim wshnetwork 



Set wshnetwork = CreateObject ("Wscript.network")
StrComputer = WshNetwork.ComputerName 
If IsEmpty(StrComputer) Then Wscript.Quit





Set WshNetwork = CreateObject("WScript.Network") 
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer") 
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48) 
Set WshShell = WScript.CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 


 outFile = "C:\scripts\Printers" & StrComputer




Set objOutputFile = objFSO.OpenTextFile(outFile, ForAppending, True)
For Each objPrinter in colInstalledPrinters 
strTest = Left(objPrinter.Name, 2) 
objOutputFile.WriteLine(objPrinter.Name) 
objfile.close
Next 

Set objPrinter = WshNetwork.EnumPrinterConnections
'Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True) 
If objPrinter.Count = 0 Then
WScript.Echo "No Printers Mapped "
else
For intDrive = 0 To (objPrinter.Count -1) Step 2
intNetLetter = IntNetLetter +1
printer = "UNC Path " & objPrinter.Item(intDrive) & " = " & objPrinter.Item(intDrive +1) & " Printer : " & intDrive
objOutputFile.WriteLine(printer)
Next
end if
objOutputFile.Close``*

1 Answers1

0
Invalid procedure call or argument  

You passed an invalid parameter in your procedure call. This could be because the parameter was out of range, or contained invalid data. Alternately, you may have invoked a procedure at an unexpected time.

To correct this error

Verify that the parameters being passed to the procedure are valid.

Verify that you are calling the function at an appropriate time.

My guess is this line is an ilegal filename.

outFile = "C:\scripts\Printers" & StrComputer

On my computer this is c:\scripts\PrintersSerenity which is probably not right that your text file is called PrintersSerenity without an extension.

Noodles
  • 1,981
  • 1
  • 11
  • 4
  • See `objPrinters = object.EnumPrinterConnections Arguments object WshNetwork object. objPrinters Variable that holds the network printer mapping information. ` for inbuilt vbs/wsh printer commands. – Noodles Sep 18 '14 at 00:27