public static void Test_Word() { Test_ComputeTotalDifference(); using (var w = new WordWrapper()) { w.App.Visible = true; w.Doc = w.Docs.Add(); w.Sel = w.App.Selection; w.SetNarrowMargins(); w.Title("Inventory Monitor (PIMON)"); w.Heading2("1. Overall Discrepancies"); w.AddClipboard(); w.Heading2("2. Carat Discrepancies"); w.Doc.SaveAs(@"C:\docs\WordTest.docx"); } }The code breaks here (a method of WordWrapper, see below
public void AddClipboard() { App.Activate(); System.Threading.Thread.Sleep(5000); Sel.PasteExcelTable(false,false, false); var t = Sel.Tables[Sel.Tables.Count]; t.Range.Select(); Sel.set_Style(App.ActiveDocument.Styles["No Spacing"]); t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent); Doc.Paragraphs.Add().Range.Select(); Sel.set_Style(App.ActiveDocument.Styles["No Spacing"]); Sel.TypeText("\n"); }Here's the method that creates the excel sheet and copies it to the clipboard
public static void Test_ComputeTotalDifference() { Serialization.Deserialize(); using (var xl = new ExcelWrapper()) { xl.wb = xl.wbs.Add(); xl.ws = xl.wb.ActiveSheet; ... snip ... xl.ws.UsedRange.CopyPicture(); } }Here's the excelwrapper class that gets initialized with a Using
public class ExcelWrapper : IDisposable { public enum Formats {decimal2,decimal0,date,text} public Excel.Application app; public Excel.Workbooks wbs; public Excel.Workbook wb; public Excel.Worksheets wss; public Excel.Worksheet ws; public Excel.Range rng; public bool mustQuit = true; CultureInfo originalCulture; Thread thread; bool disposed = false; #region "Dispose" public ExcelWrapper() { thread = Thread.CurrentThread; originalCulture = thread.CurrentCulture; app = new Excel.Application(); app.DisplayAlerts = false; wbs = app.Workbooks; } void releaseObject(object o) { if (o == null) { return; } System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o); } public void Dispose() { if (disposed){ return; } // try { wb.Close(); } finally { } if (mustQuit) { app.Quit(); } releaseObject(rng); releaseObject(ws); releaseObject(wss); releaseObject(wb); releaseObject(wbs); thread.CurrentCulture = originalCulture; disposed = true; } ~ExcelWrapper() { Dispose(); }Similarly here is the Wordwrapper.
public WordWrapper() { thread = Thread.CurrentThread; App = new Word.Application(); App.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; Docs = App.Documents; } void releaseObject(object o) { if (o == null) { return; } System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o); } public void Dispose() { if (disposed) { return; } // try { wb.Close(); } finally { } if (MustQuit) { App.Quit(); } releaseObject(Doc); releaseObject(Docs); disposed = true; } ~WordWrapper() { Dispose(); }