我只是想从一个文件夹批量打印大量文件。该文件夹包含多种文件类型,我只想调用等同于Right-Click > Print
的打印方法。
似乎我应该能够使用InvokeVerb
对象的Shell32.FolderItem
方法执行此操作。
所以,我无法弄清楚为什么,当我运行下面的代码时,没有任何打印。
有什么想法吗?
(GetFolder
只是Shell32.BrowseForFolder
函数的包装器,它返回所选文件夹的路径。该函数没有问题。对于测试,您只需替换为文件夹的路径。)
Sub printFOO()
Dim shApp As Shell32.Shell
Dim srFSO As Scripting.FileSystemObject
Dim strPath As String
Dim shFIcol As Shell32.FolderItems
Dim shFIx As Shell32.FolderItem
Dim shFLDx As Shell32.Folder
Dim lngX As Long
Set shApp = New Shell32.Shell
Set srFSO = New Scripting.FileSystemObject
strPath = GetFolder("Choose a folder...")
Set shFLDx = shApp.NameSpace(strPath)
Set shFIcol = shFLDx.Items()
For Each shFIx In shFIcol
'For lngX = 0 To shFIx.Verbs.Count
'Debug.Print shFIx.Verbs.ITEM(lngX).Name
'Next
'msgbox("printing "&shFIx.name)
shFIx.InvokeVerb ("&Print")
DoEvents
Next
End Sub
答案 0 :(得分:2)
这是一个使用稍微不同的方法来完成它的程序。它还列出了可用的动词。
HelpMsg = vbcrlf & " ShVerb" & vbcrlf & vbcrlf & " David Candy 2014" & vbcrlf & vbcrlf & " Lists or runs an explorer verb (right click menu) on a file or folder" & vbcrlf & vbcrlf & " ShVerb <filename> [verb]" & vbcrlf & vbcrlf & " Used without a verb it lists the verbs available for the file or folder" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & " The program lists most verbs but only ones above the first separator" & vbcrlf & " of the menu work when used this way" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & " The Properties verb can be used. However the program has to keep running" & vbcrlf & " to hold the properties dialog open. It keeps running by displaying" & vbcrlf & " a message box."
Set objShell = CreateObject("Shell.Application")
Set Ag = WScript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If Ag.count = 0 then
wscript.echo " ShVerb - No file specified"
wscript.echo HelpMsg
wscript.quit
Else If Ag.count = 1 then
If LCase(Replace(Ag(0),"-", "/")) = "/h" or Replace(Ag(0),"-", "/") = "/?" then
wscript.echo HelpMsg
wscript.quit
End If
ElseIf Ag.count > 2 then
wscript.echo vbcrlf & " ShVerb - To many parameters" & vbcrlf & " Use quotes around filenames and verbs containing spaces" & vbcrlf
wscript.echo HelpMsg
wscript.quit
End If
If fso.DriveExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetFileName(Ag(0)))
' Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
Set objFolderItem = objFolder.self
msgbox ag(0)
ElseIf fso.FolderExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
ElseIf fso.fileExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
Else
wscript.echo " ShVerb - " & Ag(0) & " not found"
wscript.echo HelpMsg
wscript.quit
End If
Set objVerbs = objFolderItem.Verbs
'If only one argument list verbs for that item
If Ag.count = 1 then
For Each cmd in objFolderItem.Verbs
If len(cmd) <> 0 then CmdList = CmdList & vbcrlf & replace(cmd.name, "&", "")
Next
wscript.echo mid(CmdList, 2)
'If two arguments do verbs for that item
ElseIf Ag.count = 2 then
For Each cmd in objFolderItem.Verbs
If lcase(replace(cmd, "&", "")) = LCase(Ag(1)) then
wscript.echo Cmd.doit
Exit For
End If
Next
'Properties is special cased. Script has to stay running for Properties dialog to show.
If Lcase(Ag(1)) = "properties" then
WSHShell.AppActivate(ObjFolderItem.Name & " Properties")
msgbox "This message box has to stay open to keep the " & ObjFolderItem.Name & " Properties dialog open."
End If
End If
End If
答案 1 :(得分:1)
您不需要文件夹浏览对话框的FSO。尝试 shApp.BrowseForFolder(0,“选择要从”,0,0开始打印的文件夹)。通过这种方法,您可以直接获得shell文件夹对象。此外,您可能需要检查每个文件夹项目,如果是文件或文件夹。
Sub printFgOO()
Dim shApp As Shell32.Shell
Dim shFIcol As Shell32.FolderItems
Dim shFIx As Shell32.FolderItem
Dim shFLDx As Shell32.Folder
Dim lngX As Long
Set shApp = New Shell32.Shell
Set shFLDx = shApp.BrowseForFolder(0, "Select Folder to print from", 0, 0)
Set shFIcol = shFLDx.Items()
For Each shFIx In shFIcol
If Not shFIx.IsFolder Then ' Print only if is file
shFIx.InvokeVerb ("&Print")
DoEvents
End If
Next
End Sub
按照here所述尝试函数ShellExecute!
答案 2 :(得分:0)
好的,所以我仍然没有回答为什么 InvokeVerb
方法无法打印,但我现在有办法使用{打印文件{1}} @Radek建议的功能。
想我会在这里分享我的工作代码。随意提出改进建议;)
ShellExecute
答案 3 :(得分:0)
如果您愿意使用PowerShell来解决此问题:
#Save this as Print-Files.ps1
[CmdletBinding()]
param(
[Property(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string]$Path)
foreach($file in (Get-ChildItem $path ))
{
Start-Process –FilePath $file.FullName –Verb Print
}