Пример #1
0
        /*Avendo già memmorizzato i dati ( Cliente1.Nome = txtNome.Text; e Cliente1.Cognome = txtCognome.Text;)*/
        public static void scrittura(bool intestazione, Cliente cliente)
        {
            object missing = System.Reflection.Missing.Value;

            Word.Application wordApp = new Word.Application();
            wordApp.Visible     = false;
            wordApp.WindowState = Word.WdWindowState.wdWindowStateNormal;

            Word.Document wordDoc = wordApp.Documents.Add();

            var path = @"" + Path.GetFullPath("image.jpg");

            Word.Range range;

            //se la CheckBox dell'intestazione è checkata
            if (intestazione)
            {
                //incollo l'immagine come intestazione
                foreach (Microsoft.Office.Interop.Word.Section section in wordDoc.Sections)
                {
                    //Get the header range and add the header details.
                    Microsoft.Office.Interop.Word.Range docRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    docRange.Fields.Add(docRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);

                    // Create an InlineShape in the InlineShapes collection where the picture should be added later
                    // It is used to get automatically scaled sizes.
                    Word.InlineShape autoScaledInlineShape = docRange.InlineShapes.AddPicture(path);
                    float            scaledWidth           = autoScaledInlineShape.Width;
                    float            scaledHeight          = autoScaledInlineShape.Height;
                    autoScaledInlineShape.Delete();

                    // Create a new Shape and fill it with the picture
                    Word.Shape newShape = wordDoc.Shapes.AddShape(1, 0, 0, scaledWidth, scaledHeight);
                    newShape.Fill.UserPicture(path);

                    // Convert the Shape to an InlineShape and optional disable Border
                    Word.InlineShape finalInlineShape = newShape.ConvertToInlineShape();
                    finalInlineShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

                    // Cut the range of the InlineShape to clipboard
                    finalInlineShape.Range.Cut();

                    // And paste it to the target Range
                    docRange.Paste();
                }
            }

            //formattazione testo (se la modifichi occhio alle tabulazioni sotto)
            wordDoc.Content.SetRange(0, 0);
            wordDoc.Content.Font.Size = 15;

            // Get the current date
            DateTime thisDay = DateTime.Today;

            // intestazione con nome cognome e data:
            wordDoc.Content.Text = "Preventivo\t\t\t\t\t\t\t\t\t\t" + thisDay.ToString("d") + "\nRiferimento: " + cliente.Nome + " " + cliente.Cognome + "\n" + Environment.NewLine;

            int       i = 0;
            ArrayList totali_singoli_prodotti = new ArrayList(); //creo un array formato dal totale dei prodotti per riempire una tab alla fine

            //per ogni prodotto presente nell'array:
            foreach (Prodotto prodotto in cliente.ListaProdotti)
            {
                if (!string.IsNullOrWhiteSpace(cliente.ListaProdotti[i].NomeProdotto))
                {
                    range = GetRange(wordDoc);

                    //stampo la tabella con le caratteristiche del singolo prodotto
                    Word.Table tab = wordDoc.Tables.Add(range, 9, 3, ref missing, ref missing);
                    tab.Borders.Enable = 1;

                    // tabella prodotto n
                    foreach (Word.Row row in tab.Rows)
                    {
                        foreach (Word.Cell cell in row.Cells)
                        {
                            cell.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                            cell.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

                            //la colonna con la descrizione del prodotto la metto con sfondo grigio
                            if (cell.ColumnIndex == 1 || cell.RowIndex == 1)
                            {
                                cell.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray25;
                            }

                            //prima riga
                            if (cell.RowIndex == 1)
                            {
                                if (cell.ColumnIndex == 2)
                                {
                                    cell.Range.Text = "Sconto :";
                                }
                                if (cell.ColumnIndex == 3)
                                {
                                    cell.Range.Text = "Totale :";
                                }
                            }

                            //scrivo le descrizioni nella colonna a sinistra
                            if (cell.ColumnIndex == 1)
                            {
                                if (cell.RowIndex == 2)
                                {
                                    if (cliente.ListaProdotti[i].NumeroPezzi != 1)
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].NumeroPezzi + " x " + cliente.ListaProdotti[i].NomeProdotto + ":";
                                    }
                                    else
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].NomeProdotto + ":";
                                    }
                                }
                                if (cell.RowIndex == 3)
                                {
                                    cell.Range.Text = "Sconto :";
                                }
                                if (cell.RowIndex == 4)
                                {
                                    cell.Range.Text = "Costo :";
                                }
                                if (cell.RowIndex == 5)
                                {
                                    cell.Range.Text = "Ricarica :";
                                }
                                if (cell.RowIndex == 6)
                                {
                                    cell.Range.Text = "Posa :";
                                }
                                if (cell.RowIndex == 7)
                                {
                                    cell.Range.Text = "Trasporti :";
                                }
                                if (cell.RowIndex == 8)
                                {
                                    cell.Range.Text = "Accessori :";
                                }
                                if (cell.RowIndex == 9)
                                {
                                    cell.Range.Text = "Lavorazioni :";
                                }
                            }

                            //colonna in centro
                            if (cell.ColumnIndex == 2)
                            {
                                if (cell.RowIndex == 3)
                                {
                                    cell.Range.Text = cliente.ListaProdotti[i].Sconto + "%";
                                }
                                if (cell.RowIndex == 5)
                                {
                                    cell.Range.Text = cliente.ListaProdotti[i].Ricarica + "%";
                                }
                            }

                            //colonna destra
                            if (cell.ColumnIndex == 3)
                            {
                                if (cell.RowIndex == 2)
                                {
                                    cell.Range.Text = cliente.ListaProdotti[i].PrezzoListino.ToString();
                                }
                                if (cell.RowIndex == 4)
                                {
                                    cell.Range.Text = cliente.ListaProdotti[i].Costo.ToString();
                                }
                                if (cell.RowIndex == 6)
                                {
                                    if (cliente.ListaProdotti[i].UnitaPosa)
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].NumeroPezzi + " x " + cliente.ListaProdotti[i].Posa;
                                    }
                                    else
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].Posa.ToString();
                                    }
                                }
                                if (cell.RowIndex == 7)
                                {
                                    if (cliente.ListaProdotti[i].UnitaTrasporto)
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].NumeroPezzi + " x " + cliente.ListaProdotti[i].Trasporto;
                                    }
                                    else
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].Trasporto.ToString();
                                    }
                                }
                                if (cell.RowIndex == 8)
                                {
                                    if (cliente.ListaProdotti[i].UnitaAccessori)
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].NumeroPezzi + " x " + cliente.ListaProdotti[i].Accessori;
                                    }
                                    else
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].Accessori.ToString();
                                    }
                                }
                                if (cell.RowIndex == 9)
                                {
                                    if (cliente.ListaProdotti[i].UnitaLavorazioni)
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].NumeroPezzi + " x " + cliente.ListaProdotti[i].Lavorazioni;
                                    }
                                    else
                                    {
                                        cell.Range.Text = cliente.ListaProdotti[i].Lavorazioni.ToString();
                                    }
                                }
                            }
                        }
                    }

                    range = GetRange(wordDoc);

                    //stampo la tabella con il totale de singolo Prodotto
                    Word.Table TabTotaleProd = wordDoc.Tables.Add(range, 1, 3, ref missing, ref missing);
                    TabTotaleProd.Borders.Enable = 1;
                    foreach (Word.Row row in TabTotaleProd.Rows)
                    {
                        foreach (Word.Cell cell in row.Cells)
                        {
                            //sistemo il background e allineo il testo

                            cell.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                            cell.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

                            //se è nella prima colonna (testo):
                            if (cell.ColumnIndex == 1)
                            {
                                cell.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray25;
                                cell.Range.Text      = "Totale Prodotto:";
                                cell.Range.Font.Bold = 1;
                            }

                            //se è nella terza colonna:
                            else if (cell.ColumnIndex == 3)
                            {
                                cell.Range.Text = cliente.ListaProdotti[i].Totale + "";
                            }
                        }
                    }

                    //aggiungo il totale di questo prodotto all'array
                    totali_singoli_prodotti.Add(cliente.ListaProdotti[i].Totale);
                }
                i++;
            }
            i     = 0;
            range = GetRange(wordDoc);

            //riassunto tutti totali
            Word.Table tabTotFattura = wordDoc.Tables.Add(range, cliente.ListaProdotti.Count + 2, 2, ref missing, ref missing);
            tabTotFattura.Borders.Enable = 1;
            //scorro tutti i prodotti
            foreach (Prodotto prodotto in cliente.ListaProdotti)
            {
                if (!string.IsNullOrWhiteSpace(cliente.ListaProdotti[i].NomeProdotto))
                {
                    foreach (Word.Row row in tabTotFattura.Rows)
                    {
                        foreach (Word.Cell cell in row.Cells)
                        {
                            cell.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                            cell.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            if (cell.ColumnIndex == 1)
                            {
                                cell.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray25;
                                if (cell.RowIndex == 1)
                                {
                                    cell.Range.Text = "Prodotti:";
                                }
                                if (cell.RowIndex == i + 2)
                                {
                                    cell.Range.Text = cliente.ListaProdotti[i].NomeProdotto;
                                }
                            }
                            if (cell.ColumnIndex == 2)
                            {
                                if (cell.RowIndex == 1)
                                {
                                    cell.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray25;
                                    cell.Range.Text = "Totale:";
                                }
                                if (cell.RowIndex == i + 2)
                                {
                                    cell.Range.Text = cliente.ListaProdotti[i].Totale.ToString();
                                }
                                if (cell.RowIndex == cliente.ListaProdotti.Count + 2)
                                {
                                    cell.Range.Text = cliente.TotaleFattura + " €";
                                }
                            }
                        }
                    }
                }
                i++;
            }


            //tabella con il riassunto dei totali:
            //chiudo
            var savingPath = @"" + Path.GetFullPath("ArchivioWord") + "\\" + cliente.Nome + cliente.Cognome + cliente.Indice + ".docx";

            wordDoc.SaveAs(savingPath);
            wordDoc.Close();
            wordApp.Quit();
        }
Пример #2
0
        private void bgWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (bgWorker1.CancellationPending)
                {
                    e.Cancel = true;
                }
                else
                {
                    //Lấy thông tin từ class static

                    if (InfoTest.DanhSachNhomCauHoi.Count == 0)
                    {
                        //MessageBox.Show("dto moi them = " + InfoTest.DanhSachNhomCauHoi[0].DuongDan + "; điểm = " + InfoTest.DanhSachNhomCauHoi[0].Diem.ToString());
                        MessageBox.Show("Không có dữ liệu", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    //
                    string sKyThi           = InfoTest.sKyThi;
                    string sMonThi          = InfoTest.sMonThi;
                    string sDoiTuong        = InfoTest.sDoiTuong;
                    string sHinhThucThi     = InfoTest.sHinhThucThi;
                    string sThoiGian        = InfoTest.sThoiGian;
                    string sNgayThangNamThi = InfoTest.sNgayThi + "/" + InfoTest.sThangThi + "/" + InfoTest.sNamThi;
                    int    iSoDe            = InfoTest.iSoDe;//_soDe là số đề cần tạo ra  Được tính từ MaDeTu và MaDeDen
                    //
                    ArrayList dsCauHoiBoDe = new ArrayList();
                    int       iSoCauMax;                                                    //n là số câu max trong danh sách câu nhóm 1
                    int       k = 0;
                    int       iPhanTramCongViec = 0;                                        //j là số % công việc đang thực hiện
                    //TẠO DANH SACH CÂU HỎI CHO BỘ ĐỀ ĐẢM BẢO YÊU CẦU
                    for (int iNhom = 0; iNhom < InfoTest.DanhSachNhomCauHoi.Count; iNhom++) //Làm từng đề lần lượt
                    {
                        iSoCauMax = InfoTest.DanhSachNhomCauHoi[iNhom].SoCau;
                        List <int> tmp = CreateListTest(iSoCauMax, iSoDe, k);
                        //string ds1 = "";
                        //foreach (int m in tmp)
                        //{
                        //    ds1 = ds1 + m.ToString() + " ";
                        //}
                        //MessageBox.Show("DS câu nhóm " + iNhom.ToString() +": " + ds1);
                        dsCauHoiBoDe.Add(tmp);
                        k++;
                    }
                    //% mở đề thi mẫu
                    bgWorker1.ReportProgress(5);
                    //Mở MS Word rồi tạo file đề thi để chuẩn bị copy nội dung
                    Word.Application wordApp = new Word.Application();
                    wordApp.Visible = false;
                    object filename = InfoTest.sThuMucDeMau + "\\DeThiMau3.docx";

                    List <int>    processesbeforegen = getRunningProcesses();
                    Word.Document docDeThiMau        = 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);;

                    Word.Document emptyDoc = null;  //Tạo tệp tin đề thi
                    emptyDoc = wordApp.Documents.Add();
                    Word.Document emptyDoc1 = null; //Tạo tệp tin đáp án đề thi
                    emptyDoc1 = wordApp.Documents.Add();
                    string sTenDe = "";
                    for (int _iSoDe = 0; _iSoDe < iSoDe; _iSoDe++) //Xét từng đề
                    {
                        //% Tạo đề thi thứ nhất
                        bgWorker1.ReportProgress((_iSoDe + 1) * 100 / iSoDe);
                        iPhanTramCongViec      = iPhanTramCongViec + 100 / iSoDe;//Cập nhật % hiện đã hoàn thành
                        InfoTest.iDeThiDangTao = _iSoDe + 1;
                        //
                        docDeThiMau.Activate();
                        //Copy nội dung file mẫu
                        docDeThiMau.Sections.First.Range.Copy();
                        //Paste vào tệp tin ĐỀ THI
                        emptyDoc.Activate();
                        oRng   = emptyDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                        oPara2 = emptyDoc.Content.Paragraphs.Add(ref oRng);
                        oPara2.Range.PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdFormatOriginalFormatting); //paste tiêu đề và giữa nguyên định dạng
                        //Find and replace:
                        sTenDe = InfoTest.DanhSachTenDeThi[_iSoDe];                                                           //Lấy tên đề trong danh sách tên đề
                        //
                        while (sTenDe.Length < 2)
                        {
                            sTenDe = '0' + sTenDe;
                        }
                        this.FindAndReplace(wordApp, "$KYTHI$", sKyThi);
                        this.FindAndReplace(wordApp, "$MONTHI$", sMonThi);
                        this.FindAndReplace(wordApp, "$DOITUONG$", sDoiTuong);
                        this.FindAndReplace(wordApp, "$HINHTHUCTHI$", sHinhThucThi);
                        this.FindAndReplace(wordApp, "$THOIGIAN$", sThoiGian);
                        this.FindAndReplace(wordApp, "$NGAYTHI$", sNgayThangNamThi);
                        this.FindAndReplace(wordApp, "$MADE$", sTenDe.ToString());
                        //Paste vào tệp tin ĐÁP ÁN ĐỀ THI
                        emptyDoc1.Activate();
                        oRng   = emptyDoc1.Bookmarks.get_Item(ref oEndOfDoc).Range;
                        oPara2 = emptyDoc1.Content.Paragraphs.Add(ref oRng);
                        oPara2.Range.PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdFormatOriginalFormatting); //paste tiêu đề và giữa nguyên định dạng
                        //Find and replace:
                        sTenDe = InfoTest.DanhSachTenDeThi[_iSoDe];                                                           //Lấy tên đề trong danh sách tên đề
                        //
                        while (sTenDe.Length < 3)
                        {
                            sTenDe = '0' + sTenDe;
                        }
                        this.FindAndReplace(wordApp, "$KYTHI$", sKyThi);
                        this.FindAndReplace(wordApp, "$MONTHI$", sMonThi);
                        this.FindAndReplace(wordApp, "$DOITUONG$", sDoiTuong);
                        this.FindAndReplace(wordApp, "$HINHTHUCTHI$", sHinhThucThi);
                        this.FindAndReplace(wordApp, "$THOIGIAN$", sThoiGian);
                        this.FindAndReplace(wordApp, "$NGAYTHI$", sNgayThangNamThi);
                        this.FindAndReplace(wordApp, "$MADE$", sTenDe.ToString());
                        //Paste nội dung chi tiết
                        for (int i = 0; i < InfoTest.DanhSachNhomCauHoi.Count; i++)            //Mở từng nhóm file
                        {
                            object        _fileNhom = InfoTest.DanhSachNhomCauHoi[i].DuongDan; //Lấy đường dẫn nhóm file
                            Word.Document docNhom   = null;
                            docNhom = wordApp.Documents.Open(ref _fileNhom, 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);
                            docNhom.Activate();
                            //---------------Paste ĐỀ THI
                            List <int> tt = (List <int>)dsCauHoiBoDe[i];
                            docNhom.Tables[tt[_iSoDe]].Rows[1].Cells[1].Range.Copy();//Lấy cùng vị trí câu trong từng nhóm câu hỏi
                            // Select the last table.
                            // For this demo, the table has size of 3*4 (row * column).
                            int totalTables = emptyDoc.Tables.Count;
                            Microsoft.Office.Interop.Word.Table tab   = emptyDoc.Tables[totalTables];
                            Microsoft.Office.Interop.Word.Range range = tab.Range;
                            // Moves the cursor to the first cell of target row.
                            range.Start = tab.Rows[tab.Rows.Count].Cells[1].Range.Start;
                            range.End   = range.Start;
                            // Paste values to target row.
                            range.Paste();
                            // Insert a new row after the last row.
                            if (i != InfoTest.DanhSachNhomCauHoi.Count - 1)
                            {
                                tab.Rows.Add(ref missing);
                            }
                            //-------------Kết thúc Paste ĐỀ THI
                            //-------------------------------------------------
                            //---------------Paste ĐÁP ÁN ĐỀ THI
                            List <int> tt1 = (List <int>)dsCauHoiBoDe[i];
                            docNhom.Tables[tt1[_iSoDe]].Range.Copy();//Lấy cùng vị trí câu trong từng nhóm câu hỏi
                            // Select the last table.
                            // For this demo, the table has size of 3*4 (row * column).
                            int totalTables1 = emptyDoc1.Tables.Count;
                            Microsoft.Office.Interop.Word.Table tab1   = emptyDoc1.Tables[totalTables];
                            Microsoft.Office.Interop.Word.Range range1 = tab1.Range;
                            // Moves the cursor to the first cell of target row.
                            range1.Start = tab1.Rows[tab1.Rows.Count].Cells[1].Range.Start;
                            range1.End   = range1.Start;
                            // Paste values to target row.
                            range1.Paste();
                            //-------------Kết thúc Paste ĐÁP ÁN ĐỀ THI

                            //
                            #region "Sao chép chỉ nội dung của cell"
                            //List<int> tt = (List<int>)dsCauHoiBoDe[i];
                            //var copyFrom = docNhom.Tables[tt[_iSoDe]].Rows[1].Cells[1].Range;//Lấy cùng vị trí câu trong từng nhóm câu hỏi
                            //int start = emptyDoc.Content.End - 1;
                            //int end = emptyDoc.Content.End;
                            //var copyTo = emptyDoc.Range(start, end);
                            //copyFrom.MoveEnd(Microsoft.Office.Interop.Word.WdUnits.wdCharacter, -1);
                            //copyTo.FormattedText = copyFrom.FormattedText;
                            #endregion

                            //----------------ĐỀ THI_Thay thế chữ "Câu hỏi" bằng "Câu 1 (2 điểm)"
                            string tenCauHoi = "";
                            int    tenCau;
                            tenCau    = i + 1;
                            tenCauHoi = InfoTest.sTenCau + " " + tenCau.ToString() + " (" + InfoTest.DanhSachNhomCauHoi[i].Diem + " điểm)";
                            emptyDoc.Activate();
                            this.FindAndReplace(wordApp, "Câu hỏi", tenCauHoi);
                            //-------------------ĐỀ THI-kết thúc
                            //----------------ĐÁP ÁN ĐỀ THI_Thay thế chữ "Câu hỏi" bằng "Câu 1 (2 điểm)"

                            emptyDoc1.Activate();
                            this.FindAndReplace(wordApp, "Câu hỏi", tenCauHoi);
                            //-------------------ĐÁP ÁN ĐỀ THI-kết thúc
                            ////Thêm 1 mark dòng
                            //copyTo.InsertParagraphAfter();
                            //copyTo.SetRange(end, end);
                            //Close Document:
                            docNhom.Close(ref missing, ref missing, ref missing);
                            //Kết thúc đọc file nhóm
                        }
                        //-----------ĐỀ THI - Thêm giải thích
                        oRng = emptyDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                        if (InfoTest.sGiaiThich != "")                         //Nếu có thêm giải thích
                        {
                            oPara = emptyDoc.Content.Paragraphs.Add(ref oRng); //add paragraph at end of document
                            //oPara.Range.Font.Name = "Arial";
                            oPara.Range.Text        = InfoTest.sGiaiThich;     //"Không được sử dụng tài liệu"; //add some text in paragraph
                            oPara.Alignment         = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            oPara.Range.Font.Bold   = 1;
                            oPara.Format.SpaceAfter = 10; //define some style
                            oPara.Range.InsertParagraphAfter();
                        }
                        if (_iSoDe != iSoDe - 1)
                        {
                            ////Thêm 1 ngắt trang, chuyển sang đề tiếp theo
                            wrdRng = emptyDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                            wrdRng.InsertBreak(ref oPageBreak);
                        }
                        //-------------kết thúc ĐỀ THI - Thêm giải thích
                        //-----------ĐÁP ÁN ĐỀ THI - Thêm giải thích
                        oRng = emptyDoc1.Bookmarks.get_Item(ref oEndOfDoc).Range;
                        if (InfoTest.sGiaiThich != "")                          //Nếu có thêm giải thích
                        {
                            oPara = emptyDoc1.Content.Paragraphs.Add(ref oRng); //add paragraph at end of document
                            //oPara.Range.Font.Name = "Arial";
                            oPara.Range.Text        = InfoTest.sGiaiThich;      //"Không được sử dụng tài liệu"; //add some text in paragraph
                            oPara.Alignment         = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            oPara.Range.Font.Bold   = 1;
                            oPara.Format.SpaceAfter = 10; //define some style
                            oPara.Range.InsertParagraphAfter();
                        }
                        if (_iSoDe != iSoDe - 1)
                        {
                            ////Thêm 1 ngắt trang, chuyển sang đề tiếp theo
                            wrdRng = emptyDoc1.Bookmarks.get_Item(ref oEndOfDoc).Range;
                            wrdRng.InsertBreak(ref oPageBreak);
                        }
                        //-------------kết thúc ĐÁP ÁN ĐỀ THI - Thêm giải thích
                    }
                    //Save as: filename ĐỀ THI
                    if (!System.IO.Directory.Exists(InfoTest.sViTriLuuDeThi))
                    {
                        InfoTest.sViTriLuuDeThi = InfoTest.sThuMucGoc;
                    }
                    string name   = sMonThi + string.Format("{0: ddMMyyyyhhmmss}", DateTime.Now);
                    object saveAs = InfoTest.sViTriLuuDeThi + "\\" + name + ".docx";
                    emptyDoc.SaveAs2(ref saveAs, 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, ref missing, ref missing);
                    emptyDoc.Close(ref missing, ref missing, ref missing);
                    //Save as: filename ĐÁP ÁN ĐỀ THI
                    object saveAs1 = InfoTest.sViTriLuuDeThi + "\\" + name + "_dap_an" + ".docx";
                    emptyDoc1.SaveAs2(ref saveAs1, 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, ref missing, ref missing);
                    emptyDoc1.Close(ref missing, ref missing, ref missing);
                    //
                    docDeThiMau.Close(); //Đóng đề thi mẫu
                    wordApp.Quit();      //Thoát MS Word
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi 1: " + ex.Message + " source:" + ex.Source);
            }
            finally
            {
                processesaftergen = getRunningProcesses();
                killProcesses(processesbeforegen, processesaftergen);
            }
        }