예제 #1
0
        private void btnHuongDanSuDung_Click(object sender, EventArgs e)
        {
            if (cboModule.EditValue == null)
            {
                return;
            }
            if (cboVersion.SelectedIndex < 0)
            {
                return;
            }

            string localPath        = "";
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                localPath = fbd.SelectedPath;
            }
            else
            {
                return;
            }

            string code     = TextUtils.ToString(cboModule.EditValue).Substring(5, 5);
            string name     = TextUtils.ToString(grvCboModule.GetFocusedRowCellValue(colModuleName)).ToUpper();
            string version  = string.Format("{0:00}", (int)cboVersion.SelectedValue);
            string _pPathDT = Application.StartupPath + "\\Templates\\PhongKyThuat\\";

            using (WaitDialogForm fWait = new WaitDialogForm("Vui lòng chờ trong giây lát...", "Đang tạo biểu mẫu"))
            {
                Word.Application word = new Word.Application();
                Word.Document    doc  = new Word.Document();
                try
                {
                    string fileName = "D0." + version + "." + code + ".docx";
                    try
                    {
                        File.Copy(_pPathDT + "HDSD.docx", (localPath + "\\" + fileName), true);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        doc.Close();
                        word.Quit();
                        return;
                    }


                    doc = word.Documents.Open(localPath + "\\" + fileName);
                    doc.Activate();

                    TextUtils.FindReplaceAnywhere(word, "<code>", Path.GetFileNameWithoutExtension(fileName));
                    TextUtils.FindReplaceAnywhere(word, "<name>", name);
                    //TextUtils.FindReplaceAnywhere(word, "<thongso>", TextUtils.ToString(grvCboModule.GetFocusedRowCellValue(colSpecifications)));
                    //TextUtils.RepalaceText(doc, "<thongso>", TextUtils.ToString(grvCboModule.GetFocusedRowCellValue(colSpecifications)));
                    doc.Save();

                    Process.Start(localPath + "\\" + fileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    doc.Close();
                    word.Quit();
                    TextUtils.ReleaseComObject(doc);
                    TextUtils.ReleaseComObject(word);
                }
            }
        }