using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.Office.Interop.Word; namespace OfficePrinter { public class OnDocOffice { //Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application(); public void PrintWordToPDF(string filename, string pdfname) { Application appWord = new Application(); Microsoft.Office.Interop.Word.Document wordDocument = new Microsoft.Office.Interop.Word.Document(); wordDocument = appWord.Documents.Open(filename); appWord.Visible = true; wordDocument.PrintOut( OutputFileName: pdfname, PrintToFile: true); wordDocument.Close(); wordDocument = null; appWord.Quit(SaveChanges: false); appWord = null; return; } public string PrintInWord(string Filename, string printmacro, int OfficeSleep) { string error = ""; Application appWord = new Application(); appWord.NormalTemplate.Saved = true; Microsoft.Office.Interop.Word.Document wordDocument = new Microsoft.Office.Interop.Word.Document(); wordDocument = appWord.Documents.Open(Filename); Thread.Sleep(OfficeSleep); appWord.Visible = true; appWord.Activate(); wordDocument.Activate(); Microsoft.Office.Interop.Word.Window window = appWord.ActiveWindow; window.SetFocus(); window.Activate(); appWord.WindowState = WdWindowState.wdWindowStateMinimize; appWord.WindowState = WdWindowState.wdWindowStateMaximize; if (printmacro == "") return ""; try { appWord.Run(printmacro); } catch (Exception e) { error = e.Message.ToString()+" / "+printmacro; } Thread.Sleep(OfficeSleep); wordDocument.Close(); wordDocument = null; appWord.Quit(SaveChanges: false); appWord = null; return error; } } }