示例#1
0
        public void CombinarCorrespondencia(string RutaArchivo, DataTable datos)
        {
            if (!System.IO.File.Exists(RutaArchivo))
            {
                throw new Exception("No existe el archivo especificado.\nArchivo no encontrado: " + RutaArchivo);
            }

            // Add a new document.
            wrdDoc = wrdApp.Documents.Add(RutaArchivo);
            wrdDoc.Select();

            var wrdSelection = wrdApp.Selection;
            var wrdMailMerge = wrdDoc.MailMerge;

            // Create a MailMerge Data file.
            CreateMailMergeDataFile(datos);

            // Perform mail merge.
            wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
            wrdMailMerge.Execute(ref oFalse);

            // Close the original form document.
            wrdDoc.Saved   = true;
            wrdApp.Visible = true;
        }
示例#2
0
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter      = "ZTree Pay Files (*.pay)|*.pay|All Files|*.*";
            openFileDialog1.Multiselect = true;
            bool?userClickedOK = openFileDialog1.ShowDialog();

            if (userClickedOK == true)
            {
                readPayFile(openFileDialog1.FileName);
                Microsoft.Office.Interop.Word.Selection wrdSelection;
                Microsoft.Office.Interop.Word.MailMerge wrdMailMerge;

                string StrToAdd;

                wrdApp         = new Microsoft.Office.Interop.Word.Application();
                wrdApp.Visible = true;

                wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing);
                wrdDoc.Select();

                wrdSelection = wrdApp.Selection;



                foreach (Participant partip in this.adat)
                {
                    wrdSelection.ParagraphFormat.Alignment =
                        Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
                    wrdSelection.Font.Size = 26;
                    wrdSelection.Font.Name = "Arial";
                    wrdSelection.TypeText("Receipt \r\n Seat number: " + partip.boothno);
                    wrdSelection.Font.Size = 10;
                    wrdSelection.Font.Name = "Times New Roman";

                    InsertLines(1);
                    wrdSelection.ParagraphFormat.Alignment =
                        Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

                    StrToAdd = "\r\n COGNITION AND BEHAVIOR LAB";

                    try
                    {
                        string imgLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "1.png");
                        wrdSelection.InlineShapes.AddPicture(imgLocation);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Image file (1.png) not found!");
                    }
                    wrdSelection.TypeText(StrToAdd);

                    wrdSelection.TypeText("\r\nName: " + partip.name);
                    wrdSelection.TypeText("\r\nCPR number: " + partip.cpr);
                    wrdSelection.TypeText("\r\nYour earnings in today’s experiment: " + partip.profit + " kr. \r\n");

                    wrdSelection.ParagraphFormat.Alignment =
                        Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;

                    wrdSelection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
                    StrToAdd = "Aarhus University will automatically transfer the amount you earn into your NemKonto (for this we need your CPR number). This is simply your existing bank account, into which all payments from the public sector flow (e.g. tax refunds or SU student grants). Alexander Koch and his team will start registering the payments with the administration of Aarhus University this week. Then the administrative process might take between 2-6 weeks. You can contact Alexander Koch by email ([email protected]) if you want information on the payment process.";
                    wrdSelection.TypeText(StrToAdd);
                    wrdSelection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
                    InsertLines(1);
                    StrToAdd = "According to Danish law, Aarhus University reports payments to the tax authorities. Please note that, depending on your personal income tax rate, taxes will be deducted from the amount of money you earn in this study. That is, the amount you will receive might be lower than the pre-tax earnings stated above.";
                    wrdSelection.TypeText(StrToAdd);
                    InsertLines(1);

                    wrdSelection.ParagraphFormat.Alignment =
                        Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                    Object objDate = "dddd, MMMM dd, yyyy";
                    wrdSelection.InsertDateTime(ref objDate, ref oFalse, ref oMissing,
                                                ref oMissing, ref oMissing);

                    wrdSelection.InlineShapes.AddHorizontalLineStandard();
                    wrdSelection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
                }
            }
        }
示例#3
0
        private void mbPrint_Click(object sender, EventArgs e)
        {
            try
            {
                Word.Selection wrdSelection;
                Word.Table     wrdTable;
                string         StrToAdd;

                // открываем Word и делаем его видимым
                wrdApp         = new Word.Application();
                wrdApp.Visible = true;

                // создаем документ
                wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing);
                wrdDoc.Select();
                wrdSelection = wrdApp.Selection;

                // Заголовок документа.
                StrToAdd = "Отчет";
                wrdSelection.ParagraphFormat.Alignment =
                    Word.WdParagraphAlignment.wdAlignParagraphCenter;
                wrdSelection.TypeText(StrToAdd);

                InsertLines(2);

                //выравнивание по правому краю и ставка времени и даты
                wrdSelection.ParagraphFormat.Alignment =
                    Word.WdParagraphAlignment.wdAlignParagraphRight;

                Object objDate = "Дата составления: dddd, MMMM dd, yyyy" + " г.";
                wrdSelection.InsertDateTime(ref objDate, ref oFalse, ref oMissing,
                                            ref oMissing, ref oMissing);

                InsertLines(2);

                if (this.metroGrid1.Rows.Count <= 0)
                {
                    StrToAdd = "Таблица пустая";
                    wrdSelection.TypeText(StrToAdd);
                }
                else
                {
                    wrdTable = wrdDoc.Tables.Add(wrdSelection.Range, this.metroGrid1.Rows.Count + 1, 4,
                                                 ref oMissing, ref oMissing);
                    //Параметры таблицы
                    wrdTable.Columns[1].SetWidth(100, Word.WdRulerStyle.wdAdjustNone);
                    wrdTable.Columns[2].SetWidth(170, Word.WdRulerStyle.wdAdjustNone);
                    wrdTable.Columns[3].SetWidth(100, Word.WdRulerStyle.wdAdjustNone);
                    wrdTable.Columns[4].SetWidth(111, Word.WdRulerStyle.wdAdjustNone);

                    wrdTable.Rows[1].Cells.Shading.BackgroundPatternColorIndex =
                        Word.WdColorIndex.wdGray25;

                    wrdTable.Rows[1].Range.Bold = 1;

                    wrdTable.Cell(1, 1).Range.Paragraphs.Alignment =
                        Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    //отрисовка таблицы
                    wrdTable.Borders[Word.WdBorderType.wdBorderBottom].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wrdTable.Borders[Word.WdBorderType.wdBorderTop].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wrdTable.Borders[Word.WdBorderType.wdBorderVertical].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wrdTable.Borders[Word.WdBorderType.wdBorderHorizontal].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wrdTable.Borders[Word.WdBorderType.wdBorderLeft].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;
                    wrdTable.Borders[Word.WdBorderType.wdBorderRight].LineStyle =
                        Word.WdLineStyle.wdLineStyleSingle;


                    FillRow(wrdDoc, 1, "Наименование", "Остаток",
                            "Необходимо", "Надо заказать");

                    for (int i = 2; i <= this.metroGrid1.Rows.Count + 1; i++)
                    {
                        if ((Convert.ToInt32(this.metroGrid1[4, i].Value) - Convert.ToInt32(this.metroGrid1[5, i].Value)) >= 0)
                        {
                            FillRow(wrdDoc, i, this.metroGrid1[0, i].Value.ToString(), this.metroGrid1[5, i].Value.ToString(),
                                    this.metroGrid1[4, i].Value.ToString(), Convert.ToString(Convert.ToInt32(this.metroGrid1[4, i].Value) - Convert.ToInt32(this.metroGrid1[5, i].Value)));
                        }
                        else
                        {
                            FillRow(wrdDoc, i, this.metroGrid1[0, i].Value.ToString(), this.metroGrid1[5, i].Value.ToString(),
                                    this.metroGrid1[4, i].Value.ToString(), "Не требуется");
                        }
                    }
                }
            }
            catch (Exception ex) { }
        }
        /// <summary>
        /// generate the mail merge document and print it to default print queue
        /// </summary>
        private void PrintMailMerge()
        {
            Word.MailMergeFields wrdMergeFields;

            // Create an instance of Word  and make it visible.
            _wrdApp = new Word.Application {
                Visible = false
            };

            // Add a new document.
            _wrdDoc = _wrdApp.Documents.Add(ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing);
            _wrdDoc.Select();

            var wrdSelection = _wrdApp.Selection;
            var wrdMailMerge = _wrdDoc.MailMerge;

            // Perform mail merge. setup the default queue
            _wrdApp.ActivePrinter = _defaultPrintQueue;

            // Create a MailMerge Data file.
            CreateMailMergeDataFile();

            if (_mailMergeData.Format == MailMergeFormat.Letter)
            {
                GenerateLetter(wrdSelection, wrdMailMerge, out wrdMergeFields);
            }
            else
            {
                try
                {
                    _wrdDoc.PageSetup.PaperSize    = Word.WdPaperSize.wdPaperEnvelope10;
                    _wrdDoc.PageSetup.Orientation  = Word.WdOrientation.wdOrientLandscape;
                    _wrdDoc.PageSetup.BottomMargin = 36.28f;
                    _wrdDoc.PageSetup.RightMargin  = 36.28f;
                    _wrdDoc.PageSetup.TopMargin    = 36.28f;
                    _wrdDoc.PageSetup.LeftMargin   = 36.28f;

                    _wrdDoc.Envelope.DefaultOrientation = Word.WdEnvelopeOrientation.wdLeftLandscape;
                    _wrdDoc.Envelope.DefaultFaceUp      = true;
                    GenerateEnvelope(wrdSelection, wrdMailMerge, out wrdMergeFields);
                }
                catch (System.Runtime.InteropServices.COMException comException)
                {
                    _wrdDoc.Close(ref _oFalse, ref _oMissing, ref _oMissing);

                    // Release References.
                    _wrdDoc = null;
                    _wrdApp = null;

                    File.Delete(_tempDataSourceFileName);
                    throw new Exception(comException.Message);
                }
            }

            try
            {
                wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter;
                //wrdMailMerge.HighlightMergeFields = true;
                wrdMailMerge.MainDocumentType = _mailMergeData.Format == MailMergeFormat.Letter ? Word.WdMailMergeMainDocType.wdFormLetters : Word.WdMailMergeMainDocType.wdEnvelopes;
                wrdMailMerge.Execute(ref _oFalse);
            }
            catch (System.Runtime.InteropServices.COMException comException)
            {
                _wrdDoc.Close(ref _oFalse, ref _oMissing, ref _oMissing);

                // Release References.
                _wrdDoc = null;
                _wrdApp = null;

                // Clean up temp file.
                File.Delete(_tempDataSourceFileName);
                throw new Exception(comException.Message);
                //throw new ActivityFailedException("Failed to print the envelopes", comException);
            }

            // Close the original form document.
            _wrdDoc.Saved = true;
            _wrdDoc.Close(ref _oFalse, ref _oMissing, ref _oMissing);

            // Release References.
            _wrdDoc = null;
            _wrdApp = null;

            // Clean up temp file.
            File.Delete(_tempDataSourceFileName);
        }
示例#5
0
        private void button1_Click(object sender, RoutedEventArgs e)
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "ZTree Pay Files (*.pay)|*.pay|All Files|*.*";      
            openFileDialog1.Multiselect = true;
            bool? userClickedOK = openFileDialog1.ShowDialog();

            if (userClickedOK == true)
            {
                readPayFile(openFileDialog1.FileName);
                Microsoft.Office.Interop.Word.Selection wrdSelection;
                Microsoft.Office.Interop.Word.MailMerge wrdMailMerge;

                string StrToAdd;

                wrdApp = new Microsoft.Office.Interop.Word.Application();
                wrdApp.Visible = true;

                wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                wrdDoc.Select();

                wrdSelection = wrdApp.Selection;



                foreach (Participant partip in this.adat)
                {

                    wrdSelection.ParagraphFormat.Alignment =
                        Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
                    wrdSelection.Font.Size = 26;
                    wrdSelection.Font.Name = "Arial";
                    wrdSelection.TypeText("Receipt \r\n Seat number: " + partip.boothno);
                    wrdSelection.Font.Size = 10;
                    wrdSelection.Font.Name = "Times New Roman";

                    InsertLines(1);
                    wrdSelection.ParagraphFormat.Alignment =
                       Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

                    StrToAdd = "\r\n COGNITION AND BEHAVIOR LAB";

                    try
                    {                  
                        string imgLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "1.png");
                        wrdSelection.InlineShapes.AddPicture(imgLocation);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Image file (1.png) not found!");
                    }
                    wrdSelection.TypeText(StrToAdd);
     
                    wrdSelection.TypeText("\r\nName: " + partip.name);
                    wrdSelection.TypeText("\r\nCPR number: " + partip.cpr);
                    wrdSelection.TypeText("\r\nYour earnings in today’s experiment: " + partip.profit + " kr. \r\n");

                    wrdSelection.ParagraphFormat.Alignment =
                        Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;

                    wrdSelection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
                    StrToAdd = "Aarhus University will automatically transfer the amount you earn into your NemKonto (for this we need your CPR number). This is simply your existing bank account, into which all payments from the public sector flow (e.g. tax refunds or SU student grants). Alexander Koch and his team will start registering the payments with the administration of Aarhus University this week. Then the administrative process might take between 2-6 weeks. You can contact Alexander Koch by email ([email protected]) if you want information on the payment process.";
                    wrdSelection.TypeText(StrToAdd);
                    wrdSelection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
                    InsertLines(1);
                    StrToAdd = "According to Danish law, Aarhus University reports payments to the tax authorities. Please note that, depending on your personal income tax rate, taxes will be deducted from the amount of money you earn in this study. That is, the amount you will receive might be lower than the pre-tax earnings stated above.";
                    wrdSelection.TypeText(StrToAdd);
                    InsertLines(1);

                    wrdSelection.ParagraphFormat.Alignment =
                        Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                    Object objDate = "dddd, MMMM dd, yyyy";
                    wrdSelection.InsertDateTime(ref objDate, ref oFalse, ref oMissing,
                        ref oMissing, ref oMissing);

                    wrdSelection.InlineShapes.AddHorizontalLineStandard();
                    wrdSelection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
                }


            }
        }
示例#6
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            Word.Selection       wrdSelection;
            Word.MailMerge       wrdMailMerge;
            Word.MailMergeFields wrdMergeFields;
            Word.Table           wrdTable;
            string StrToAdd;

            // Create an instance of Word  and make it visible.
            wrdApp         = new Word.Application();
            wrdApp.Visible = true;

            // Add a new document.
            wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
                                          ref oMissing, ref oMissing);
            wrdDoc.Select();

            wrdSelection = wrdApp.Selection;
            wrdMailMerge = wrdDoc.MailMerge;

            // Create a MailMerge Data file.
            CreateMailMergeDataFile();

            // Create a string and insert it into the document.
            StrToAdd = "State University\r\nElectrical Engineering Department";
            wrdSelection.ParagraphFormat.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphCenter;
            wrdSelection.TypeText(StrToAdd);

            InsertLines(4);

            // Insert merge data.
            wrdSelection.ParagraphFormat.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphLeft;
            wrdMergeFields = wrdMailMerge.Fields;
            wrdMergeFields.Add(wrdSelection.Range, "FirstName");
            wrdSelection.TypeText(" ");
            wrdMergeFields.Add(wrdSelection.Range, "LastName");
            wrdSelection.TypeParagraph();

            wrdMergeFields.Add(wrdSelection.Range, "Address");
            wrdSelection.TypeParagraph();
            wrdMergeFields.Add(wrdSelection.Range, "CityStateZip");

            InsertLines(2);

            // Right justify the line and insert a date field
            // with the current date.
            wrdSelection.ParagraphFormat.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphRight;

            Object objDate = "dddd, MMMM dd, yyyy";

            wrdSelection.InsertDateTime(ref objDate, ref oFalse, ref oMissing,
                                        ref oMissing, ref oMissing);

            InsertLines(2);

            // Justify the rest of the document.
            wrdSelection.ParagraphFormat.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphJustify;

            wrdSelection.TypeText("Dear ");
            wrdMergeFields.Add(wrdSelection.Range, "FirstName");
            wrdSelection.TypeText(",");
            InsertLines(2);

            // Create a string and insert it into the document.
            StrToAdd = "Thank you for your recent request for next " +
                       "semester's class schedule for the Electrical " +
                       "Engineering Department. Enclosed with this " +
                       "letter is a booklet containing all the classes " +
                       "offered next semester at State University.  " +
                       "Several new classes will be offered in the " +
                       "Electrical Engineering Department next semester.  " +
                       "These classes are listed below.";
            wrdSelection.TypeText(StrToAdd);

            InsertLines(2);

            // Insert a new table with 9 rows and 4 columns.
            wrdTable = wrdDoc.Tables.Add(wrdSelection.Range, 9, 4,
                                         ref oMissing, ref oMissing);
            // Set the column widths.
            wrdTable.Columns[1].SetWidth(51, Word.WdRulerStyle.wdAdjustNone);
            wrdTable.Columns[2].SetWidth(170, Word.WdRulerStyle.wdAdjustNone);
            wrdTable.Columns[3].SetWidth(100, Word.WdRulerStyle.wdAdjustNone);
            wrdTable.Columns[4].SetWidth(111, Word.WdRulerStyle.wdAdjustNone);
            // Set the shading on the first row to light gray.
            wrdTable.Rows[1].Cells.Shading.BackgroundPatternColorIndex =
                Word.WdColorIndex.wdGray25;
            // Bold the first row.
            wrdTable.Rows[1].Range.Bold = 1;
            // Center the text in Cell (1,1).
            wrdTable.Cell(1, 1).Range.Paragraphs.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphCenter;

            // Fill each row of the table with data.
            FillRow(wrdDoc, 1, "Class Number", "Class Name",
                    "Class Time", "Instructor");
            FillRow(wrdDoc, 2, "EE220", "Introduction to Electronics II",
                    "1:00-2:00 M,W,F", "Dr. Jensen");
            FillRow(wrdDoc, 3, "EE230", "Electromagnetic Field Theory I",
                    "10:00-11:30 T,T", "Dr. Crump");
            FillRow(wrdDoc, 4, "EE300", "Feedback Control Systems",
                    "9:00-10:00 M,W,F", "Dr. Murdy");
            FillRow(wrdDoc, 5, "EE325", "Advanced Digital Design",
                    "9:00-10:30 T,T", "Dr. Alley");
            FillRow(wrdDoc, 6, "EE350", "Advanced Communication Systems",
                    "9:00-10:30 T,T", "Dr. Taylor");
            FillRow(wrdDoc, 7, "EE400", "Advanced Microwave Theory",
                    "1:00-2:30 T,T", "Dr. Lee");
            FillRow(wrdDoc, 8, "EE450", "Plasma Theory",
                    "1:00-2:00 M,W,F", "Dr. Davis");
            FillRow(wrdDoc, 9, "EE500", "Principles of VLSI Design",
                    "3:00-4:00 M,W,F", "Dr. Ellison");

            // Go to the end of the document.
            Object oConst1 = Word.WdGoToItem.wdGoToLine;
            Object oConst2 = Word.WdGoToDirection.wdGoToLast;

            wrdApp.Selection.GoTo(ref oConst1, ref oConst2, ref oMissing, ref oMissing);
            InsertLines(2);

            // Create a string and insert it into the document.
            StrToAdd = "For additional information regarding the " +
                       "Department of Electrical Engineering, " +
                       "you can visit our Web site at ";
            wrdSelection.TypeText(StrToAdd);
            // Insert a hyperlink to the Web page.
            Object oAddress = "http://www.ee.stateu.tld";
            Object oRange   = wrdSelection.Range;

            wrdSelection.Hyperlinks.Add(oRange, ref oAddress, ref oMissing,
                                        ref oMissing, ref oMissing, ref oMissing);
            // Create a string and insert it into the document
            StrToAdd = ".  Thank you for your interest in the classes " +
                       "offered in the Department of Electrical " +
                       "Engineering.  If you have any other questions, " +
                       "please feel free to give us a call at " +
                       "555-1212.\r\n\r\n" +
                       "Sincerely,\r\n\r\n" +
                       "Kathryn M. Hinsch\r\n" +
                       "Department of Electrical Engineering \r\n";
            wrdSelection.TypeText(StrToAdd);

            // Perform mail merge.
            wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
            wrdMailMerge.Execute(ref oFalse);

            // Close the original form document.
            wrdDoc.Saved = true;
            wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);


            // Release References.
            wrdSelection   = null;
            wrdMailMerge   = null;
            wrdMergeFields = null;
            wrdDoc         = null;
            wrdApp         = null;

            // Clean up temp file.
            System.IO.File.Delete("C:\\DataDoc.doc");
        }