好的,所以对于为什么该InvokeVerb
方法无法打印,我仍然没有答案,但是我现在确实有一种方法可以使用ShellExecute
@Radek 建议的功能来打印文件。
想我会在这里分享我的工作代码。随时提出改进建议;)
Option Explicit
Public Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteW" (ByVal hWnd As Long, _
ByVal lpOperation As LongPtr, _
ByVal lpFile As LongPtr, _
ByVal lpParameters As LongPtr, _
ByVal lpDirectory As LongPtr, _
ByVal nShowCmd As Long) As Long
Public Const SW_HIDE As Long = 0
'Hides the window and activates another window.
Public Const SW_MAXIMIZE As Long = 3
'Maximizes the specified window.
Public Const SW_MINIMIZE As Long = 6
'Minimizes the specified window and activates the next top-level window in the z-order.
Public Const SW_RESTORE As Long = 9
'Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
Public Const SW_SHOW As Long = 5
'Activates the window and displays it in its current size and position.
Public Const SW_SHOWDEFAULT As Long = 10
'Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.
Public Const SW_SHOWMAXIMIZED As Long = 3
'Activates the window and displays it as a maximized window.
Public Const SW_SHOWMINIMIZED As Long = 2
'Activates the window and displays it as a minimized window.
Public Const SW_SHOWMINNOACTIVE As Long = 7
'Displays the window as a minimized window. The active window remains active.
Public Const SW_SHOWNA As Long = 8
'Displays the window in its current state. The active window remains active.
Public Const SW_SHOWNOACTIVATE As Long = 4
'Displays a window in its most recent size and position. The active window remains active.
Public Const SW_SHOWNORMAL As Long = 1
'Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
Public Enum shexActions
shexEDIT
shexEXPLORE
shexFIND
shexOPEN
shexPRINT
End Enum
Private Function getShellAction(ByRef enACTION As shexActions) As String
Select Case enACTION
Case shexActions.shexEDIT
getShellAction = "EDIT"
Case shexActions.shexEXPLORE
getShellAction = "EXPLORE"
Case shexActions.shexFIND
getShellAction = "FIND"
Case shexActions.shexOPEN
getShellAction = "OPEN"
Case shexActions.shexprint
getShellAction = "PRINT"
End Select
End Function
Public Function ShellEx(ByRef strFILE As String, _
Optional ByRef lngWINDOWHANDLE As Long = 0, _
Optional ByRef shexACTION As shexActions = (-1), _
Optional ByRef strPARAMETERS As String, _
Optional ByRef strDIRECTORY As String, _
Optional ByRef lngSHOWCOMMAND As Long = 0) As Long
Dim lngReturnCheck As Long
lngReturnCheck = (-1)
lngReturnCheck = ShellExecute(hWnd:=lngWINDOWHANDLE, lpOperation:=StrPtr(getShellAction(shexACTION)), lpFile:=StrPtr(strFILE), lpParameters:=StrPtr(strPARAMETERS), lpDirectory:=StrPtr(strDIRECTORY), nShowCmd:=lngSHOWCOMMAND)
While lngReturnCheck = (-1)
DoEvents
Wend
ShellEx = lngReturnCheck
End Function
Sub printBAR()
Dim shFIcol As Shell32.FolderItems
Dim shFIx As Shell32.FolderItem
Dim shFLDx As Shell32.Folder
Dim lngX As Long
Set shFLDx = GetFolder("Choose a folder...", True)
Set shFIcol = shFLDx.Items()
For Each shFIx In shFIcol
lngX = ShellEx(shFIx.Path, , shexPRINT)
Debug.Print lngX
Next
End Sub