2

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
Dennis
  • 1,528
  • 2
  • 16
  • 31
  • 1
    You asked ParseName() to parse a file, a single item. A FolderItem, not FolderItems. Do consider doing this the .NET way, Process.Start() using the ProcessStartInfo.Verb property. – Hans Passant Nov 16 '16 at 15:49
  • Thanks for your comment Hans, thats what I tried initially, but then I get a dialoge with a 'okey' button before it's send to the printer... – Dennis Nov 17 '16 at 07:35
  • Hans you did point me in the right direction; Dim MyFile As FolderItems2 had to be: Dim MyFile As FolderItem, and it runs like a charm. Thank you again Hans, you're the king! – Dennis Nov 17 '16 at 13:31

0 Answers0