我需要reemplace和打印多次在一個wordDocument。所以我在Word文檔中有一個模板。我如何使word文檔不保存時更換使用C#
- 1 .-我打開模板
- 2:我reemplace的一些信息
- 3 .-我發送打印文檔。
- 4.-關閉wordDocument。
問題是,當我reemplace信息並關閉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);
}
}
}
我也太好了 - 閱讀幫助:-) –
那是正確的馬庫斯,謝謝你的回答 – Matvi