1

我使用一个函数来简化我的应用程序中的打印。这里是:

public void print(string fileName, string variables, string values, string template)
        {
            // CREATE TEXT FILE FOR PRINTING


        FileInfo fi = new FileInfo(fileName);
        Object oMissing = System.Reflection.Missing.Value;
        Object oTrue = true;
        Object oFalse = false;

        Word.Application _wordApp = new Word.Application();
        Word.Document oDoc = _wordApp.Documents.Add(template);

        try
        {
            // Check if file already exists. If yes, delete it. 
            if (fi.Exists)
            {
                fi.Delete();
            }

            // Create a new file 
            using (StreamWriter sw = fi.CreateText())
            {
                sw.WriteLine(variables);
                sw.WriteLine(values);
            }


            _wordApp.Visible = true;
            oDoc.MailMerge.MainDocumentType = Word.WdMailMergeMainDocType.wdFormLetters;
            oDoc.MailMerge.OpenDataSource(fileName, false, false, true);

            oDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
            oDoc.MailMerge.Execute(false);
            oDoc.MailMerge.Application.PrintOut();
        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.ToString());
        }
        finally
        {
            //CLEAR RESOURCES
            ((Word._Document)oDoc).Close(ref oFalse, ref oMissing, ref oMissing);
            ((Word._Application)_wordApp).Quit();
        }

    }

它看起来不错,因为我可以使用 Mail Merge 并使事情变得更容易。我打开一个打开文件对话框,以便用户可以选择template他将用于邮件合并的文件。但是随后,应用程序打开template文件时没有任何数据Merge Fields,然后执行邮件合并,打开另一个文档,其中包含所有数据Merge Fields

任何想法为什么会发生这种情况?这是打开文件对话框的代码:

                //CHOOSE TEMPLATE FILE
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Microsoft Word Template (*.dotx)|*.dotx";
                string filter = ofd.Filter;
                ofd.Multiselect = false;
                ofd.Title = "Open Template File";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (ofd.SafeFileName == "payslip.dotx")
                    {
                        //RETREIVE VALUES
                        var db = new DBConnect();
                        string[] values = new string[20];
                        bool print = false;
                        OleDbCommand cmd = null;
                        OleDbDataReader dr = null;
                        try
                        {
                            if (db.OpenConnection() == true)
                            {
                                string query = "SELECT * FROM employee WHERE employee_ID = " + idTxtBox.Text + "";
                                cmd = new OleDbCommand(query, db.mycon);
                                dr = cmd.ExecuteReader();
                                while (dr.Read())
                                {
                                    values[2] = (dr["employeeName"].ToString());
                                    values[3] = (dr["withTax"].ToString());
                                    values[4] = (dr["sss"].ToString());
                                    values[5] = (dr["pagIbig"].ToString());
                                    values[6] = (dr["pHealthGov"].ToString());
                                    values[7] = (dr["pCareOff"].ToString());
                                    values[8] = (dr["loan_sss"].ToString());
                                    values[9] = (dr["loan_pagIbig"].ToString());
                                    values[10] = (dr["loan_koti"].ToString());
                                    values[11] = (dr["tardinessAbscences"].ToString());
                                    values[12] = totalDeductionsTxtBox.Text;
                                    values[13] = (dr["overTime"].ToString());
                                    values[14] = (dr["leave"].ToString());
                                }
                                print = true;
                            }
                        }
                        catch (OleDbException ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            if (dr.IsClosed == false)
                            {
                                dr.Close();
                            }
                            db.CloseConnnection();
                        }

                        if (print == true)
                        {
                            // PRINTING
                            var p = new printClass();
                            p.print(@"C:\IT Box Incorporated\Payroll\payslipCSV.csv",
                                    "date_issued,employee_name,tax,sss,pagibig,phg,pco,sssloan,pagibigloan,kotiloan,late,total,ot,leave",
                                    "" + (DateTime.Now.Date.ToString("MMM") + " " + DateTime.Now.Date.ToString("yyyy")) + "," + values[2] + "," + values[3] + "," + values[4] + "," + values[5] + "," + values[6] + "," + values[7] + "," + values[8] + "," + values[9] + "," + values[10] + "," + values[11] + "," + values[12] + "," + values[13] + "," + values[14] + "",
                                    ofd.FileName);
                        }
                        else
                        {
                            MessageBox.Show("Print Failed!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Print Failed! Wrong File!");
                    }
                }
                else
                {
                    MessageBox.Show("Print Failed!");
                }
4

1 回答 1

1

在 Windows 7 中,如果您启用了预览,则在文件打开期间 Windows 将尝试预览文档并启动新的 winword 进程。

在 Windows 7 中禁用预览模式。

于 2013-04-23T13:33:41.147 回答