VBS 文件不打印

VBS file not printing

我是 VBS 的新手,我正在尝试创建一个脚本来对文件夹中的一些文件进行排序,如果满足条件,应该输出 MsgBox 并打印文件。 MsgBox 部分工作但打印功能不工作。感谢您的指导。

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

InvokeVerbExShellFolderItem 对象的一个​​方法: https://docs.microsoft.com/en-us/windows/win32/shell/shellfolderitem-object

在您的示例脚本中,file 变量只是一个 File 对象(通过 Scripting.FileSystemObject 获得),而不是 ShellFolderItem 对象。

获取 ShellFolderItem 对象的一种方法是使用 Shell.Application,然后使用当前文件夹调用 NameSpace 方法,然后使用文件名。

例如,尝试将第 14 行 file.InvokeVerbEx ("Print") 替换为:

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

希望对您有所帮助。

我最终使用 wsShell.Run 文件打开记事本并让最终用户打印;因为该文件需要以横向模式打印,我不确定记事本是否有任何 sendkeys 命令。

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,wsShell
  my_counter = 0
  Set wsShell = Wscript.CreateObject("WScript.shell")
  Set fso = CreateObject("scripting.FileSystemObject")
  For Each file In fso.GetFolder(specify_path).Files
      If CDate(FormatDateTime(file.DateLAstModified, vbShortDate)) = Date Then
         my_counter = my_counter + 1
         wShell.Run File

      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