VBS脚本实现指定文件夹Word文档自动打印删除及触发需求问询
Hey there! Let’s work through your auto-printing problem one piece at a time—you’re close, just a few tweaks needed to get it working exactly how you want.
1. Fix: Only Print & Delete Word Documents
Your original script processes all files in the folder, but we can add a check to target only Word document types (.doc, .docx, .docm, etc.). Here’s the updated, refined code:
TargetFolder = "Z:\Desktop\work" Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(TargetFolder) Set colItems = objFolder.Items ' List of Word document extensions to target (add more if needed) wordExtensions = Array(".doc", ".docx", ".docm", ".dotx", ".dotm") For Each objItem In colItems ' Skip folders, only process actual files If Not objItem.IsFolder Then ' Get the file extension (lowercase for consistent matching) fileName = objItem.Name fileExt = LCase(Right(fileName, Len(fileName) - InStrRev(fileName, "."))) ' Check if this file is a Word document isWordDoc = False For Each ext In wordExtensions If "." & fileExt = ext Then isWordDoc = True Exit For End If Next If isWordDoc Then objItem.InvokeVerbEx ("Print") ' Wait for printing to finish (10 seconds = 10000 milliseconds) WScript.Sleep 10000 objItem.InvokeVerbEx ("delete") End If End If Next
Why your sleep 10 failed:
VBScript doesn’t use a plain sleep command—you need to use WScript.Sleep with a value in milliseconds. So 10 seconds becomes WScript.Sleep 10000 (adjust this number if your printer needs more time).
2. Auto-Trigger the Script When Files Are Added
You have two solid options here, depending on how "real-time" you need the process to be:
Option 1: Windows Task Scheduler (Simple, No Continuous Script)
This runs your script on a schedule (e.g., every minute) to check for new files—great if you don’t need instant processing:
- Open Task Scheduler > Create Basic Task
- Name it something clear (e.g., "Auto Print Word Docs")
- Set a trigger: Daily > then set to repeat every 1 minute for 24 hours
- Set an action: Start a program > browse to your saved
.vbsfile - Finish the wizard, and ensure the task runs with your user account (to access the folder and printer)
Option 2: Real-Time Folder Monitoring (VBS with FileSystemWatcher)
If you want instant processing when a file is added, use a script that runs in the background and watches the folder. Here’s how:
Set fso = CreateObject("Scripting.FileSystemObject") Set folderWatcher = CreateObject("System.IO.FileSystemWatcher") ' Configure the folder watcher folderWatcher.Path = "Z:\Desktop\work" folderWatcher.Filter = "*.doc*" ' Target all Word-related file types folderWatcher.IncludeSubdirectories = False folderWatcher.EnableRaisingEvents = True ' Function to handle new files as they're added Sub OnFileCreated(ByVal sender, ByVal e) ' Wait 2 seconds to ensure the file is fully written (avoids "file in use" errors) WScript.Sleep 2000 Set objShell = CreateObject("Shell.Application") Set targetFile = objShell.Namespace(folderWatcher.Path).ParseName(e.Name) targetFile.InvokeVerbEx ("Print") WScript.Sleep 10000 ' Wait for print job to complete targetFile.InvokeVerbEx ("delete") End Sub ' Bind the event handler to the watcher Set eventHandler = GetRef("OnFileCreated") folderWatcher.Created = eventHandler ' Keep the script running in the background Do While True WScript.Sleep 1000 Loop
- Save this as
AutoPrintWatcher.vbs - Double-click to run it (it will run silently in the background)
- To make it start automatically on Windows boot, add a shortcut to it in your Startup folder (
C:\Users\[YourName]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup)
Quick Tips to Avoid Headaches
- File Locking: Always add a short delay (like
WScript.Sleep 2000) before processing new files—this ensures the file is fully copied to the folder before your script tries to print/delete it. - Permissions: Make sure your user account has read/write access to the target folder, and that your printer is set as the default (or verify the
Printverb works with your specific printer setup).
内容的提问来源于stack exchange,提问作者mohsin117

