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

C# 使用C替换时如何使word文档不保存#,c#,printing,ms-word,C#,Printing,Ms Word,我需要在一个wordDocument中重新嵌入并打印多次。所以我在word文档中有一个模板 -我打开模板 2.-我重新植入了一些信息 3.-我发送来打印文件 -我关闭wordDocument 问题是,当我重新嵌入信息并关闭de WordDocument时,文档被保存。 我不想保存文档,因为模板无法工作 WordTarReg impToWord = new WordTarReg(); Word.Application wordApp = new Word.Application(); Word

我需要在一个wordDocument中重新嵌入并打印多次。所以我在word文档中有一个模板

  • -我打开模板
  • 2.-我重新植入了一些信息
  • 3.-我发送来打印文件
  • -我关闭wordDocument
问题是,当我重新嵌入信息并关闭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);
        }
    }
}
public Word.Document CreateWordDocument(对象文件名,Word.Application wordApp)
{
缺少对象=缺少.Value;
Word.Document aDoc=null;
如果(File.Exists((字符串)文件名))
{
对象只读=false;
对象isVisible=false;
可见=false;
aDoc=wordApp.Documents.Open(引用文件名、引用缺失、引用只读、引用缺失、,
参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,
参考缺失、参考缺失、参考缺失、参考缺失、参考缺失、参考缺失、参考缺失);
aDoc.Activate();
//MessageBox.Show(“创建的文件”);
}
其他的
{
MessageBox.Show(“缺少注册卡Microsoft Word文档”);
}
返回aDoc;
}
public void remplaceInformationOnDocument(Word.Application wordApp,Dictionary dataToPrint)
{
//网络服务
this.findandplace(wordApp,“,dataToPrint[“algo”]);
this.findandplace(wordApp,“,dataToPrint[“algo”]);
this.findandplace(wordApp,“,dataToPrint[“algo”]);
this.findandplace(wordApp,“,dataToPrint[“algo”]);
this.findandplace(wordApp,“,dataToPrint[“algo”]);
this.findandplace(wordApp,“,dataToPrint[“algo”]);
}
公共文档(Word.Document文档)
{
缺少对象=缺少.Value;
单据关闭(参考缺失、参考缺失、参考缺失);
}
公共作废打印文档(Word.Document文档,Word.Application wordApp)
{
//Word.Application wordApp=新的Word.Application();
对象拷贝数=1;
对象页=1;
对象范围=Word.WdPrintOutRange.wdPrintCurrentPage;
对象项=Word.WdPrintOutItem.wdPrintDocumentContent;
对象页面类型=Word.WdPrintOutPages.wdPrintAllPages;
object oTrue=true;
ALSE的对象=false;
缺少对象=缺少.Value;
字符串printerName=“HP打印机”;
wordApp.ActivePrinter=printerName;
if(检查ifprinterexit(printerName))
{
wordApp.ActiveDocument.PrintOut();
}
其他的
{ 
MessageBox.Show(“注册信息不存在于计算机上的注册信息不存在的临时信息验证名称。”,“广告:注册信息不存在”,MessageBoxButtons.OK,MessageBoxIcon.叹号);
int dialogResult=wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(false);
可见=false;
如果(dialogResult==1)
{
文件打印输出(参考oTrue、参考oFalse、参考范围、参考缺失、参考缺失、,
参考项目、参考副本、参考页面、参考页面类型、参考ALSE、参考oTrue、,
参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失);
}
}
}
我找到了解决方案

在文件中: 无效关闭( ref对象保存更改, 参考对象原始格式, ref对象路由文档 )

所以我改变了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);

        }

您已经编写了以下代码:

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