Pergunta

I am developing a simple VB.NET desktop app for a little printing business. It has a main WinForm, buttons for opening JPG/PDF/Word/Excel files, open associated program, print the file, capture the spool job, and finally charge the user for what is being printed, based on printer name, number of pages, size of pages and cost per page. No big deal. Hosts machines have Win7 OS.

When then user wants to print a XLS file, I Want the app to open Excel 2010, opening a file previously selected with a file dialog. And when Excel opens, go directly to the print dialog, then when the job finishes to load in the spool, I capture that event and KILL the Excel process.

My problem is:

I cant open Excel directly going to the print dialog. Excel DOES respond to the "print" verb. But it just prints with default printer. I want it to open and go to the print dialog. I do not want to just print with default printer, I do need allow the user to select desired printer, pages, copies, etc.

I'm trying to do this with the following code:

   Dim openFileDialog1 As New OpenFileDialog()
   Dim filePath As String = ""
   Dim startInfo As ProcessStartInfo

    'openFileDialog1.InitialDirectory = "c:\"
    openFileDialog1.Filter = "XLS Files (*.xls)|*.xls|XLSX Files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 1
    openFileDialog1.RestoreDirectory = True

    If (DialogResult.OK) = openFileDialog1.ShowDialog(Me) Then
        filePath = openFileDialog1.FileName
    Else
        Exit Sub
    End If

    startInfo = New ProcessStartinfo(rutaArchivo)

    With startInfo
            .FileName = filePath 
                .WindowStyle = ProcessWindowStyle.Normal 
            .Verb = "print"
            .CreateNoWindow = False
            .UseShellExecute = True

    End With

    Try
        System.Diagnostics.Process.Start(startInfo)

    Catch ex As Exception
        MsgBox(ex.ToString)

    End Try

IDE is SharpDevelop 4.3. Framework is .NET 4.0 client profile. OS is Win7.

Many thanks :)

Foi útil?

Solução

MS Excel only has the following command line switches: Excel Switches

To do what you want, you might look into Excel Interop

Outras dicas

You should be able to do this by opening the print dialog first and then using the printer selected with a verb of "PrintTo" like this:

Dim openFileDialog1 As New OpenFileDialog()
    Dim filePath As String = ""
    Dim startInfo As ProcessStartInfo

    'openFileDialog1.InitialDirectory = "c:\"
    openFileDialog1.Filter = "XLS Files (*.xls)|*.xls|XLSX Files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 1
    openFileDialog1.RestoreDirectory = True

    If (DialogResult.OK) = openFileDialog1.ShowDialog(Me) Then
        filePath = openFileDialog1.FileName
    Else
        Exit Sub
    End If

    Dim printer As String = ""
    Dim printDialog As New PrintDialog()

    If printDialog.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
        printer = printDialog.PrinterSettings.PrinterName
    End If


    startInfo = New ProcessStartInfo(filePath)

    With startInfo
        .WindowStyle = ProcessWindowStyle.Normal
        .Verb = "PrintTo"
        '.Arguments = """\\" & System.Net.Dns.GetHostName() & "\" & printer & """"
        .Arguments = """" & printer & """"
        .CreateNoWindow = False
        .UseShellExecute = True

    End With

    Try
        System.Diagnostics.Process.Start(startInfo)

    Catch ex As Exception
        MsgBox(ex.ToString)

    End Try

You may have to include the server in the Arguments (commented line).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top