I'd like to send several types of files to the printer wihtout the printer dialog;
I've found a vbs script which works and translate it to vb.net like so;
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
It goes wrong with this error message;
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)).
On this line; MyFile = MyFolder.ParseName(MyPj.FileName)
The MyPj.Filename contains the name of the file in the folder (MyPj.Path).
I also tried this example; https://msdn.microsoft.com/en-us/library/windows/desktop/bb774057%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 without succes.
Before I've used this to send files to the printer, but this presents a dialoge with a 'okey' button;
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