Reputation: 93
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
Upvotes: 0
Views: 1238
Reputation: 93
I ended up using wsShell.Run file to open notepad and let the end user print; because the file needed to be printed in landscape mode and I am note sure if Notepad had any sendkeys command.
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
Upvotes: 0
Reputation: 1436
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.
Upvotes: 1