我需要在一个wordDocument中重新插入和打印多次。所以我在word文档中有一个模板。
问题是,当我重新插入信息并关闭de WordDocument时,文档将被保存。 我不希望保存文档,因为模板不起作用。
WordTarReg impToWord = new WordTarReg();
Word.Application wordApp = new Word.Application();
Word.Document doc;
doc = impToWord.CreateWordDocument(appGlobal.directoryApp + "\\registro.docx", wordApp);
try
{
impToWord.remplaceInformationOnDocument(wordApp, data);
impToWord.printDocument(doc, wordApp);
impToWord.closeDocument(doc);
}
catch (Exception ex)
{
MessageBox.Show("Error al interntar imprimir documento");
impToWord.closeDocument(doc);
}
进入班级
public Word.Document CreateWordDocument(object filename, Word.Application wordApp)
{
object missing = Missing.Value;
Word.Document aDoc = null;
if (File.Exists((string)filename))
{
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;
aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
aDoc.Activate();
//MessageBox.Show("File created");
}
else
{
MessageBox.Show("Registration Card Microsoft Word Document is missing");
}
return aDoc;
}
public void remplaceInformationOnDocument(Word.Application wordApp, Dictionary<String,String> dataToPrint)
{
//fin de los datos obtenidos por el web service
this.FindAndReplace(wordApp, "<dataToReplace1>", dataToPrint["algo"]);
this.FindAndReplace(wordApp, "<dataToReplace2>", dataToPrint["algo"]);
this.FindAndReplace(wordApp, "<dataToReplace3>", dataToPrint["algo"]);
this.FindAndReplace(wordApp, "<dataToReplace4>", dataToPrint["algo"]);
this.FindAndReplace(wordApp, "<dataToReplace5>", dataToPrint["algo"]);
this.FindAndReplace(wordApp, "<dataToReplace6>", dataToPrint["algo"]);
}
public void closeDocument(Word.Document doc)
{
object missing = Missing.Value;
doc.Close(ref missing, ref missing, ref missing);
}
public void printDocument(Word.Document document, Word.Application wordApp)
{
//Word.Application wordApp = new Word.Application();
object copies = 1;
object pages = 1;
object range = Word.WdPrintOutRange.wdPrintCurrentPage;
object items = Word.WdPrintOutItem.wdPrintDocumentContent;
object pageType = Word.WdPrintOutPages.wdPrintAllPages;
object oTrue = true;
object oFalse = false;
object missing = Missing.Value;
String printerName = "HP PRINTER";
wordApp.ActivePrinter = printerName;
if (CheckIfPrinterExits(printerName))
{
wordApp.ActiveDocument.PrintOut();
}
else
{
MessageBox.Show("La impresora configurada para imprimir la tarjeta de registro no existe en tu computadora \n Verifica el nombre en el archivo de configuracion.","Advertencia: Impresora no existe",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
int dialogResult = wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(false);
wordApp.Visible = false;
if (dialogResult == 1)
{
document.PrintOut(ref oTrue, ref oFalse, ref range, ref missing, ref missing,
ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing, ref missing);
}
}
}
答案 0 :(得分:1)
您已编写以下代码:
public void closeDocument(Word.Document doc)
{
object missing = Missing.Value;
doc.Close(ref missing, ref missing, ref missing);
}
根据documentation,方法Close
需要3个参数:
this.Close(ref doNotSaveChanges, ref missing, ref missing);
因此,将第一个指定为Word.WdSaveOptions.wdDoNotSaveChanges
将解决您的问题。
public void closeDocument(Word.Document doc)
{
object missing = Missing.Value;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
this.Close(ref doNotSaveChanges, ref missing, ref missing);
}
答案 1 :(得分:0)
我得到了解决方案。
在文档中: void关闭( ref对象SaveChanges, ref对象OriginalFormat, ref对象RouteDocument )
所以我改变了关闭方法
public void closeDocument(Word.Document doc)
{
object missing = Missing.Value;
object dontsave = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref dontsave, ref missing, ref missing);
}