0

Basically, I want to generate a Word file, then print it.

Here's what I have so far:

private void CreateDocument()
{
    try
    {
        // Create an instance for word app
        Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();

        // Set status for word application is to be visible or not.
        winword.Visible = false;

        // Create a missing variable for missing value
        object missing = System.Reflection.Missing.Value;

        // Create a new document
        Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);

        // Add paragraph with Heading 1 style
        Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
        object styleHeading1 = "Heading 1";
        para1.Range.set_Style(ref styleHeading1);
        para1.Range.Text = "BRGY. BOLO WATER SERVICE COOPERATIVE";
        para1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
        para1.Range.InsertParagraphAfter();

        // Save the document
        filename = Application.StartupPath + @"\Disconnection\temp1.docx";
        document.SaveAs(ref filename);
        document.Close(ref missing, ref missing, ref missing);
        document = null;

        MessageBox.Show("Document created successfully !");

        if (File.Exists(filename.ToString()))
        {
            PrintDocument printDoc = new PrintDocument();
            PrinterSettings prnsetting = new PrinterSettings();

            prnsetting.PrintFileName = Application.StartupPath + @"\Disconnection\.do";
            printDoc.DocumentName = "temp1";

            printPreviewDialog1.Document = printDoc;
            printPreviewDialog1.ShowDialog(); 
        }
    }
    catch (Exception ex)
    {
        //debug purposes
        MessageBox.Show(ex.Message +"\n"+filename.ToString());
    }

}

The problem is when the print preview shows up, its only a blank page. Is there a proper way of doing this? Do I have to set up the file path for the printer?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Alpha
  • 127
  • 2
  • 14
  • 1
    [`PrintFileName`](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printersettings.printfilename) is used when printing to a file, not to a printer. I don't think you can print a Word file this way. You can _directly_ send a file to the printer using something like [this](https://stackoverflow.com/a/6106155/4934172) but that won't display a print preview. Alternatively, you might consider displaying a print preview inside Word itself since you're already using Interop. – 41686d6564 stands w. Palestine Sep 08 '18 at 13:45

1 Answers1

0

There are numerous examples of printing a word document to a printer. Below is another one. Hope this helps?

        public void PrintWord(string strFileName)
    {
        object oMissing = System.Reflection.Missing.Value;
        string strCurrentActivePrinter;

        //Start Word and open the test document.
        Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.ApplicationClass();

        Microsoft.Office.Interop.Word._Document oDoc;
        oWord.Visible = false;

        object oPath = strFileName;

        //Get and store the current activeprinter
        strCurrentActivePrinter = oWord.ActivePrinter;

        //Assign new activeprinter to Word
        oWord.ActivePrinter = PDFprinter;

        oDoc = oWord.Documents.Open(ref oPath,
            ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing);

        //print it
        object xcopies = 1;
        object xpt = false;
        object oFalse = false;

        oDoc.PrintOut(ref xpt, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref 
            xcopies, ref oMissing, ref oMissing, ref oMissing, ref 
            oMissing, ref oMissing, ref oMissing, ref oMissing, ref 
            oMissing, ref oMissing, ref oMissing);

        //close the document
        oDoc.Close(ref oFalse, ref oMissing, ref oMissing);

        oWord.Options.SaveNormalPrompt = false;
        oWord.Options.SavePropertiesPrompt = false;

        //Reset activerprinter
        oWord.ActivePrinter = strCurrentActivePrinter;

        //close word
        oWord.Quit(ref oFalse, ref oMissing, ref oMissing);

        //Drop our reference to the COM object
        //Marshal.ReleaseComObject(oWord);
        //Marshal.ReleaseThreadCache();

        oDoc = null;
        oWord = null;
    }
amyunitech
  • 31
  • 1