我想在没有打印机对话框的情况下将几种类型的文件发送到打印机;
我找到了一个可以工作的vbs脚本并将其翻译成vb.net,就像这样;
Private Sub SendToPrinter2(MyPj As PrintJob, bgw As BackgroundWorker)
Try
Dim MyFolder As Shell32.Folder2
Dim MyFile As FolderItems2
MyFolder = GetShell32Folder(MyPj.Path)
MyFile = MyFolder.ParseName(MyPj.FileName)
MyFile.InvokeVerbEx("Print")
Catch ex As Exception
bgw.ReportProgress(0, ex.Message)
End Try
End Sub
Private Function GetShell32Folder(folderPath As String) As Shell32.Folder2
Dim shellAppType As Type = Type.GetTypeFromProgID("Shell.Application")
Dim shell As [Object] = Activator.CreateInstance(shellAppType)
Return DirectCast(shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, Nothing, shell, New Object() {folderPath}), Shell32.Folder2)
End Function
此错误消息出错;
Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.FolderItems2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C94F0AD0-F363-11D2-A327-00C04F8EEC7F}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
在这条线上; MyFile = MyFolder.ParseName(MyPj.FileName)
MyPj.Filename包含文件夹(MyPj.Path)中文件的名称。
我也试过这个例子; https://msdn.microsoft.com/en-us/library/windows/desktop/bb774057%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396没有成功。
在我使用它将文件发送到打印机之前,这会显示一个带有“okey”按钮的拨号;
Private Sub SendToPrinter(MyPj As PrintJob)
Dim info As New ProcessStartInfo()
info.Verb = "print"
info.FileName = MyPj.PathFileName
info.CreateNoWindow = True
info.WindowStyle = ProcessWindowStyle.Hidden
Dim p As New Process()
p.StartInfo = info
p.Start()
p.WaitForInputIdle()
System.Threading.Thread.Sleep(3000)
If False = p.CloseMainWindow() Then
p.Kill()
End If
End Sub