using System; using System.Collections.Generic; using System.Text; using LBWordLibrary; namespace Volian.MSWord { public class WordDoc { private static LBApplicationClass _MyWordApp; public static LBApplicationClass MyWordApp { get { if (_MyWordApp == null) _MyWordApp = new LBApplicationClass(); return WordDoc._MyWordApp; } } private LBDocumentClass _MyWordDoc; public LBDocumentClass MyWordDoc { get { return _MyWordDoc; } } private string _DocName; public string DocName { get { return _DocName; } set { _DocName = value; } } public WordDoc(string docName) { DocName = docName; _MyWordDoc = MyWordApp.Documents.Open(_DocName, false); } public string Save() { try { MyWordDoc.SaveAs(DocName, LBWdSaveFormat.wdFormatDocument); return "Successful Save"; } catch (Exception ex) { return string.Format("Error during Doc save - {0}", ex.Message); } } public string Save(string docName) { DocName = docName; return Save(); } public void Close() { MyWordDoc.Close(false); } public float SetLineSpacing(float linesPerInch) // if need landscape set too: , bool landscape) { float retval = 0; LBSelection selection = MyWordApp.Selection; selection.WholeStory(); retval = selection.ParagraphFormat.LineSpacing; selection.ParagraphFormat.SpaceBefore = 0; selection.ParagraphFormat.SpaceAfter = 0; selection.ParagraphFormat.LineSpacingRule = LBWdLineSpacing.wdLineSpaceExactly; selection.ParagraphFormat.LineSpacing = 72 / linesPerInch; return retval; } public float Length { get { try { return MyWordDoc.Length; } catch (Exception ex) { Console.WriteLine("Error Getting Length, {0}", ex.Message); } return 0; } } public static void CloseApp() { if(_MyWordApp != null) { TryToQuit(); KillWordApps(); } } private static void TryToQuit() { try { _MyWordApp.Quit(false); } catch (Exception ex) { Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message); } _MyWordApp=null; } public static void KillWordApps() { try { TerminateProcesses(WordProcesses); } catch (Exception ex) { Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message); } } public static void TerminateProcesses(System.Diagnostics.Process[] wordProcesses) { foreach (System.Diagnostics.Process proc in wordProcesses) { // Determine if Word is Visible // If it is let it run // otherwise Kill it. if(proc.MainWindowTitle=="") proc.Kill(); } _MyWordApp = null; } public static System.Diagnostics.Process[] WordProcesses { get { return System.Diagnostics.Process.GetProcessesByName("WINWORD"); } } } }