Click here to Skip to main content
16,013,489 members
Home / Discussions / C#
   

C#

 
GeneralRe: Automatic printing to pdf Pin
Heath Stewart14-Oct-04 7:00
protectorHeath Stewart14-Oct-04 7:00 
GeneralRe: Automatic printing to pdf Pin
sverre.andersen19-Oct-04 3:01
sverre.andersen19-Oct-04 3:01 
GeneralRe: Automatic printing to pdf Pin
Heath Stewart19-Oct-04 5:22
protectorHeath Stewart19-Oct-04 5:22 
GeneralRe: Automatic printing to pdf Pin
sverre.andersen19-Oct-04 10:28
sverre.andersen19-Oct-04 10:28 
GeneralRe: Automatic printing to pdf Pin
sverre.andersen21-Oct-04 0:57
sverre.andersen21-Oct-04 0:57 
GeneralRe: Automatic printing to pdf Pin
Heath Stewart21-Oct-04 6:23
protectorHeath Stewart21-Oct-04 6:23 
GeneralRe: Automatic printing to pdf Pin
sverre.andersen4-Nov-04 0:06
sverre.andersen4-Nov-04 0:06 
GeneralRe: Automatic printing to pdf Pin
Heath Stewart4-Nov-04 4:55
protectorHeath Stewart4-Nov-04 4:55 
If you read the documentation for ExecWB, as well as the MSHTML command ID documentation for IDM_PRINT (same as OLECMDID_PRINT), you'd see there is no way to print to a specified printer. You can specify a custom print template, overrides to not bother the user or wait until completion, or specify a custom header, footer, and IStream (COM interface) for an Outlook Express header document.

Your only choice in this matter is to set whatever printer you want as the default, print, then set the default printer back (if you want to reset it).

If your application will only run on Windows 2000 or newer, you can P/Invoke SetDefaultPrinter:
[DllImport("winspool.dll", CharSet=CharSet.Auto)]
static extern bool SetDefaultPrinter(string printer);
You could actually set CharSet to CharSet.Unicode since this is only supported in Windows 2000 and higher (in which case NT-based Windows platforms are all Unicode), but technically the API is defined using LPCTSTR, which is platform-dependent.

If you have to support any Windows platforms (i.e., not NT-based, so 95, 98, and Me, which is the last non-Unicode Windows platform - even Windows CE is Unicode), then the easiest way is to use WMI to set the default printer:
using System;
using System.Management;
using System.Reflection;
 
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyKeyName("dev")]
 
class Printers
{
  static void Main(string[] args)
  {
    EnumeratePrinters(args.Length > 0 ? args[0] : null);
  }
 
  static void EnumeratePrinters(string defaultName)
  {
    ManagementObject defaultPrinter = null;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(
      "select * from Win32_Printer");
    foreach (ManagementObject printer in searcher.Get())
    {
      string name = printer["Name"].ToString();
      Console.WriteLine("Printer: " + name);
      if (defaultName != null &&
        string.Compare(name, defaultName, true) == 0)
        defaultPrinter = printer;
    }
 
    if (defaultPrinter != null)
    {
      Console.WriteLine("Setting default printer to \"{0}\"",
        defaultPrinter["Name"]);
      defaultPrinter.InvokeMethod("SetDefaultPrinter", null);
    }      
  }
}
This is just an example I threw together before for someone that both enumerates the printers in a platform-agnostic way, as well as sets the default printer if you specify a valid printer name on the command line.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralComboboxes with relations in a datagrid Pin
jalla7214-Oct-04 0:44
jalla7214-Oct-04 0:44 
GeneralRe: Comboboxes with relations in a datagrid Pin
Heath Stewart14-Oct-04 6:46
protectorHeath Stewart14-Oct-04 6:46 
GeneralRe: Comboboxes with relations in a datagrid Pin
jalla7214-Oct-04 20:04
jalla7214-Oct-04 20:04 
GeneralRe: Comboboxes with relations in a datagrid Pin
Heath Stewart14-Oct-04 20:14
protectorHeath Stewart14-Oct-04 20:14 
GeneralRe: Comboboxes with relations in a datagrid Pin
jalla7215-Oct-04 0:01
jalla7215-Oct-04 0:01 
GeneralRe: Comboboxes with relations in a datagrid Pin
Heath Stewart15-Oct-04 6:18
protectorHeath Stewart15-Oct-04 6:18 
QuestionHow to make the Form (at run time) behave like the Form (at design time) Pin
god4k14-Oct-04 0:33
god4k14-Oct-04 0:33 
AnswerRe: How to make the Form (at run time) behave like the Form (at design time) Pin
Heath Stewart14-Oct-04 6:50
protectorHeath Stewart14-Oct-04 6:50 
AnswerRe: How to make the Form (at run time) behave like the Form (at design time) Pin
Christian Wikander15-Oct-04 3:32
Christian Wikander15-Oct-04 3:32 
GeneralMoving a file over a network. Pin
Dylan van Heerden14-Oct-04 0:30
Dylan van Heerden14-Oct-04 0:30 
GeneralRe: Moving a file over a network. Pin
Colin Angus Mackay14-Oct-04 2:00
Colin Angus Mackay14-Oct-04 2:00 
GeneralRe: Moving a file over a network. Pin
Dylan van Heerden14-Oct-04 2:18
Dylan van Heerden14-Oct-04 2:18 
GeneralRe: Moving a file over a network. Pin
Colin Angus Mackay14-Oct-04 2:22
Colin Angus Mackay14-Oct-04 2:22 
GeneralRe: Moving a file over a network. Pin
Dylan van Heerden14-Oct-04 2:39
Dylan van Heerden14-Oct-04 2:39 
GeneralRe: Moving a file over a network. Pin
Colin Angus Mackay14-Oct-04 2:47
Colin Angus Mackay14-Oct-04 2:47 
GeneralRe: Moving a file over a network. Pin
Dylan van Heerden14-Oct-04 2:51
Dylan van Heerden14-Oct-04 2:51 
GeneralRe: Moving a file over a network. Pin
Heath Stewart14-Oct-04 6:43
protectorHeath Stewart14-Oct-04 6:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.