Search code examples
vbscriptfso

VBS file not printing


I am new to VBS and I am trying to create a script that sorts some files in a folder and, if conditions are met, should output a MsgBox as well as printing the file. The MsgBox part works but the printing functionality doesn't work. Thanks for any guidance.

Option Explicit
Dim today_date, path
today_date = Date
path = "C:\Users\MyComputer\Desktop\FORMS"
Call GetRecentFile(path)


Function GetRecentFile(specify_path)
  Dim fso, file, my_counter
  my_counter = 0
  Set fso = CreateObject("scripting.FileSystemObject")
  For Each file In fso.GetFolder(specify_path).Files
      If CDate(FormatDateTime(file.DateLAstModified, vbShortDate)) = Date Then
         file.InvokeVerbEx ("Print")
         my_counter = my_counter + 1

      End If
  Next

  If my_counter = 1 Then
     MsgBox "There is a new  file in the folder: " & path, 0, "ATTENTION !"
  ElseIf my_counter > 1 Then
     MsgBox "There are " & my_counter & "file in the folder: " & path, 0, "ATTENTION !"
  Else: MsgBox "There are no new files as of " & Now, 0, "NOTIFICATION"
  End If    


End Function

Solution

  • InvokeVerbEx is a method of the ShellFolderItem object: https://learn.microsoft.com/en-us/windows/win32/shell/shellfolderitem-object

    In your example script, the file variable is simply a File object (obtained through Scripting.FileSystemObject), not a ShellFolderItem object.

    One way to obtain a ShellFolderItem object is to use Shell.Application, then call the NameSpace method using the current folder, then call its ParseName method using the file name.

    For example, try replacing your line 14 file.InvokeVerbEx ("Print") with:

        With CreateObject("Shell.Application")
            With .NameSpace(specify_path)
                With .ParseName(file.Name)
                    .InvokeVerbEx("Print")
                End With
            End With
        End With
    

    Hope this helps.