Search code examples
c#vbaprintingdefaultadministrator

VBA : Changing Windows 7 Default Printer temporarily


is there any possibility to change the windows default printer for one "job" only? Does it work without having adminstrative permissions under Windows XP / Vista / 7?

what i want to do:

  • Default printer is Canon ABC
  • want to print via Epson XYZ
  • after printing want to having back my default printer Canon ABC again.

Best would be if it will work without administrative permissions and without "messageboxes" or UI Dialogs.

Should be used under VBA or (grudgingly) under C# / .NET


Solution

  • In C# you can print to any printer installed on the computer. You don't need to change the default printer. I don't know exactly if it's the same in VBA, but it works in C#.

    Edit:

    Due to your comment I added a little sample, hoping to lead you on the way:

    public void DoPrint()
    {
        var printDialog = new PrintDialog();
        if (printDialog.ShowDialog() == DialogResult.OK)
        {
            var printDocument = new PrintDocument
                {
                    DefaultPageSettings = { PrinterSettings = printDialog.PrinterSettings }
                };
            printDocument.PrintPage += OnPrintPage;
        }
    }
    
    private void OnPrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawString("Hello");
    }
    

    This will print "Hello" to the printer you've selected in the dialog.