首页 > 解决方案 > 生成和打印 Microsoft Word 文件

问题描述

基本上,我想生成一个 Word 文件,然后打印它。

这是我到目前为止所拥有的:

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

}

问题是当打印预览出现时,它只是一个空白页。有没有正确的方法来做到这一点?我必须为打印机设置文件路径吗?

标签: c#.netprintingms-wordoffice-interop

解决方案


有许多将 Word 文档打印到打印机的示例。下面是另一个。希望这可以帮助?

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

推荐阅读