Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Printing > Print Files

Print Files

 

Shell.Application
VBScript Code:
' Check the command line arguments
If WScript.Arguments.Unnamed.Count <> 1 Then Syntax
If WScript.Arguments.Named.Count    > 0 Then Syntax

' Check if a valid file was specified
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
strFile = WScript.Arguments(0)
If Not objFSO.FileExists( strFile ) Then Syntax
strFolder = objFSO.GetParentFolderName( strFile )
Set objFSO = Nothing

' Open the Shell Folders object
Set objShell  = CreateObject( "Shell.Application" )

' Create an object for the specified file's parent folder
Set objFolder = objShell.Namespace( strFolder )

' Create a collection for the folder's contents
Set colFiles  = objFolder.Items

' Loop through the collection to find the file specified
If colFiles.Count > 0 Then
    For Each objFile In colFiles
        If LCase( objFile.Path ) = LCase( strFile ) Then
            ' Print the file with its associated print command
            objFile.InvokeVerbEx( "Print" )
        End If
    Next
End If


Sub Syntax
    Dim strMsg
    strMsg = "Print.vbs,  Version 1.00" _
           & vbCrLf _
           & "Print a file - ANY file - on the default printer" _
           & vbCrLf & vbCrLf _
           & "Usage:  " & UCase( WScript.ScriptName ) & "  filename"  _
           & vbCrLf & vbCrLf _
           & "Where:  ""filename""  specifies the file to be printed (no wildcards)" _
           & vbCrLf & vbCrLf _
           & "Notes:  This script will only work if a print command for the"   _
           & vbCrLf _
           & "        file's associated file type is defined in the registry." _
           & vbCrLf _
           & "        When the associated program is used to open and print"   _
           & vbCrLf _
           & "        the file, the program will not be closed automatically."          _
           & vbCrLf _
           & "        This script may conflict with my DefOpen.bat script."    _
           & vbCrLf & vbCrLf _
           & "Written by Rob van der Woude" _
           & vbCrLf _
           & "http://www.robvanderwoude.com"
    WScript.Echo strMsg
    WScript.Quit 1
End Sub
Requirements:
Windows version: any
Network: N/A
Client software: Internet Explorer 4 for Windows 95/98/NT 4
Script Engine: any
Summarized: Should work in Windows 98 SE or later, or with Internet Explore 4 or later.
 
[Back to the top of this page]

page last modified: 2016-09-19; loaded in 0.0058 seconds