0

我是 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
4

2 回答 2

1

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

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

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

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

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

希望这可以帮助。

于 2020-01-17T12:59:36.540 回答
0

我最终使用 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
于 2020-01-17T19:41:05.773 回答