Matvi
Matvi

Reputation: 143

How can i make the word document dont save when replace using C#

I need to reemplace and print multiple times in one wordDocument. So i have a template in word document.

The problem is that when i reemplace the information and close de WordDocument, the documents is saved. I dont want the document saved, because the template wont work.

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

into the class

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

Upvotes: 1

Views: 524

Answers (2)

Markus Safar
Markus Safar

Reputation: 6580

You have written the following code:

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

According to the documentation the method Close takes 3 parameters:

this.Close(ref doNotSaveChanges, ref missing, ref missing);

So specifying the first one as Word.WdSaveOptions.wdDoNotSaveChanges will solve your issue.

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

Upvotes: 1

Matvi
Matvi

Reputation: 143

I GOT THE SOLUTION.

Inside the documentation : void Close( ref object SaveChanges, ref object OriginalFormat, ref object RouteDocument )

So i change the close Method

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

        }

Upvotes: 0

Related Questions