2016-02-11 70 views
1

我需要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); 
     } 
    } 
} 

回答

1

你寫了下面的代碼:

public void closeDocument(Word.Document doc) 
{ 
    object missing = Missing.Value; 
    doc.Close(ref missing, ref missing, ref missing); 
} 

按照documentationClose需要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);   
} 
0

我得到了解決。

的文檔裏面: 無效關閉( 參考對象的SaveChanges, Ref對象OriginalFormat, 參考對象RouteDocument )

所以我改變Close方法

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); 

     } 
+0

我也太好了 - 閱讀幫助:-) –

+1

那是正確的馬庫斯,謝謝你的回答 – Matvi

相關問題