none
Set Excel Default Print Settings from Visual Studio RRS feed

  • Question

  • Hi, I am trying to change the default print settings for documents printed in Excel.

    Here are the settings I am trying to get:

    • Print One Sided
    • Collated
    • Landscape Orientation
    • 11x17
    • Normal Margins (Left: 0.7" Right: 0.7")
    • Fit All Columns on One Page

    Here is the code I have so far, but nothing happens.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Excel;
    using System.Drawing.Printing;
    
    namespace ExcelPrintSettings
    {
        public partial class ThisAddIn
        {
            public event PrintPageEventHandler PrintPage;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                PrinterSettings pc = new PrinterSettings();
                pc.Duplex = Duplex.Simplex;
                pc.Collate = true;
                PrintDocument pd = new PrintDocument();
                pd.DefaultPageSettings.Landscape = true;
                pd.DefaultPageSettings.PaperSize = new PaperSize("11 x 17", 1100, 1700);
                pd.DefaultPageSettings.Margins = new Margins(70, 70, 70, 70);
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }
    

    Does anyone know how to fix this?

    • Moved by Kristin Xie Monday, January 25, 2016 6:49 AM more related to excel
    Thursday, January 21, 2016 9:55 PM

Answers

  • Where are you calling the .Print() method? And where is the document to print? Check the below example from msdn:

    public void Printing(string printer) {
      try {
        streamToPrint = new StreamReader (filePath);
        try {
          printFont = new Font("Arial", 10);
          PrintDocument pd = new PrintDocument(); 
          pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
          // Specify the printer to use.
          pd.PrinterSettings.PrinterName = printer;
    
          if (pd.PrinterSettings.IsValid) {
             pd.Print();
          } 
          else {	
             MessageBox.Show("Printer is invalid.");
          }
        } 
        finally {
          streamToPrint.Close();
        }
      } 
      catch(Exception ex) {
        MessageBox.Show(ex.Message);
      }
    }


    Fouad Roumieh

    Friday, January 22, 2016 6:17 AM