void _ExcelApplication_WorkbookBeforePrint(Microsoft.Office.Interop.Excel.Workbook Wb, ref bool Cancel) { DisplayInWatchWindow(WorkbookBeforePrint++, System.Reflection.MethodInfo.GetCurrentMethod().Name); if (ExcelHlp.HasCustomFooter()) { Actions.Excel_PageFormatting.AddFooter(); } }
public static void AddFooter() { Globals.ThisAddIn.Application.PrintCommunication = false; Workbook workBook; //Worksheet workSheet; StringBuilder sb = new StringBuilder(); try { workBook = Globals.ThisAddIn.Application.ActiveWorkbook; ExcelHlp.CalculationsOff(); foreach (Worksheet workSheet in workBook.Sheets) { sb.Length = 0; sb.Append("&5&Z&F"); // Five point font, path, and filename sb.AppendFormat("\nCreated: {0} By: {1}", ExcelHlp.GetBuiltInPropertyValue(workBook, "Creation Date"), ExcelHlp.GetBuiltInPropertyValue(workBook, "Author")); sb.AppendFormat("\nLast Saved: {0} By: {1}", ExcelHlp.GetBuiltInPropertyValue(workBook, "Last Save Time"), ExcelHlp.GetBuiltInPropertyValue(workBook, "Last author")); sb.AppendFormat("\nLast Printed: {0}", ExcelHlp.GetBuiltInPropertyValue(workBook, "Last Print Date")); if (Common.DebugMode) { Common.WriteToDebugWindow("LeftFooter:>" + sb.ToString() + "<");; } workSheet.PageSetup.LeftFooter = sb.ToString(); workSheet.PageSetup.CenterFooter = ""; sb.Length = 0; sb.Append("&5&P - &N"); // Five point font, page of pages sb.Append("\nTitle :"); sb.Append(ExcelHlp.GetBuiltInPropertyValue(workBook, "Title")); sb.Append("\nSubject: "); sb.Append(ExcelHlp.GetBuiltInPropertyValue(workBook, "Subject")); if (Common.DebugMode) { Common.WriteToDebugWindow("RightFooter:>" + sb.ToString() + "<");; } workSheet.PageSetup.RightFooter = sb.ToString(); // Indicate we have added a custom footer. // This gets checked in the BeforeClose event. if (!ExcelHlp.HasCustomFooter()) { ExcelHlp.CustomFooterExists(true); } } ExcelHlp.CalculationsOn(); } catch (Exception ex) { MessageBox.Show("AddFooter:" + ex.ToString()); } Globals.ThisAddIn.Application.PrintCommunication = true; }