PRB: The CopyFile Method Does Not Work for Printer DeviceName with Spaces on NT (252607)



The information in this article applies to:

  • Microsoft Visual Basic Learning Edition for Windows 5.0, when used with:
    • the operating system: Microsoft Windows NT
    • the operating system: Microsoft Windows 2000
  • Microsoft Visual Basic Learning Edition for Windows 6.0, when used with:
    • the operating system: Microsoft Windows NT
    • the operating system: Microsoft Windows 2000
  • Microsoft Visual Basic Professional Edition for Windows 5.0, when used with:
    • the operating system: Microsoft Windows NT
    • the operating system: Microsoft Windows 2000
  • Microsoft Visual Basic Professional Edition for Windows 6.0, when used with:
    • the operating system: Microsoft Windows NT
    • the operating system: Microsoft Windows 2000
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0, when used with:
    • the operating system: Microsoft Windows NT
    • the operating system: Microsoft Windows 2000
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0, when used with:
    • the operating system: Microsoft Windows NT
    • the operating system: Microsoft Windows 2000

This article was previously published under Q252607

SYMPTOMS

When a file is copied to the network printer with the CopyFile method of the FileSystemObject object, and the printer DeviceName contains a space, the following error occurs:
Runtime error 52: Bad file name or number

RESOLUTION

If you have administrative privileges on the server, rename the printer so that the DeviceName is one word without spaces in it.

MORE INFORMATION

Microsoft recommends that you do not have spaces in your printer DeviceNames because other problems might arise.

Step-by-Step Example

  1. On a Microsoft NT 4.0 system, add a printer with a name that contains a space.
  2. Create a text file in the C:\ folder, for example, test.txt.
  3. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  4. From the Project menu, select References, check Microsoft Scripting Runtime, and click OK.
  5. Add a CommandButton to the form.
  6. Paste the following code in the code window of Form1:
    Private Sub Command1_Click()
    Dim FSO as FileSystemObject
    set FSO = new FileSystemObject
    
    ' To print on WinNT
    FSO.CopyFile "C:\test.txt", Printer.DeviceName
    
    ' If the devicename is like this: \\servername\HPLaser400, the file prints
    ' If the devicename is \\servername\HP Laser 400, the above mentioned 
    ' run-time error occurs
    
    ' To print on Win9x
    ' FSO.CopyFile "C:\test.txt",Printer.Port
    End Sub
    					
  7. Press the F5 key to run the project, and then click on the CommandButton.
  8. The file test.txt is printed if the DeviceName is one word without spaces.
  9. Alternatively, you can use APIs to bypass the printer driver and send data to the printer. This is documented in article Q154078 listed below in the "References" section.

REFERENCES

154078 HOWTO: Send Raw Data to a Printer Using the Win32 API from VB


Modification Type:MajorLast Reviewed:5/12/2003
Keywords:kbprb kbprint KB252607