如何使用 C# 替换时使 Word 文档不保存

本文关键字:Word 文档 保存 何使用 替换 | 更新日期: 2023-09-27 18:34:11

我需要在一个word文档中重新替换和打印多次。所以我在word文档中有一个模板。

  • 1.- 我打开模板
  • 2.- 我重新放置了一些信息
  • 3.- 我发送打印文档。
  • 4.- 我关闭单词文档。

问题是当我重新放置信息并关闭 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);
        }
    }
}

如何使用 C# 替换时使 Word 文档不保存

您已经编写了以下代码:

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

根据文档,该方法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);           
}

我得到了解决方案。

在文档中:空 关闭( 引用对象保存更改, 引用对象原始格式, 引用对象路由文档)

所以我改变了关闭方法

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