【问题标题】:VBS file not printingVBS 文件不打印
【发布时间】:2020-01-17 10:32:43
【问题描述】:

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

【问题讨论】:

    标签: vbscript fso


    【解决方案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
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我最终使用 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-15
        • 2015-10-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多