示例#1
0
        // ----------------------------------------------------
        //         Replace strings in structure
        // ----------------------------------------------------
        static public void ReplaceStringInAllFiles(
            string originalFolder,
            List <TagStructure> tagList,
            Word.Application vkWordApp)
        {
            object vkMissing  = System.Reflection.Missing.Value;
            object vkReadOnly = false;
            object vkVisible  = true;
            object vkFalse    = false;

            if (Directory.Exists(originalFolder))
            {
                string[] files = Directory.GetFiles(originalFolder);
                foreach (string file in files)
                {
                    string name  = Path.GetFileName(file);
                    object xFile = file;

                    // Let's open the document
                    Word.Document vkMyDoc = vkWordApp.Documents.Open(
                        ref xFile, ref vkMissing, ref vkReadOnly,
                        ref vkMissing, ref vkMissing, ref vkMissing,
                        ref vkMissing, ref vkMissing, ref vkMissing,
                        ref vkMissing, ref vkMissing, ref vkVisible);

                    vkMyDoc.Select();

                    if (tagList.Count > 0)
                    {
                        for (int i = 0; i < tagList.Count; i++)
                        {
                            FindAndReplace(
                                tagList[i].Tag,
                                tagList[i].TagValue,
                                1,
                                vkWordApp,
                                vkMyDoc);
                        }
                    }

                    WordDocumentTasks.insertPicture(vkMyDoc, "C:\\Research\\fcm\\Resources\\FCMLogo.jpg", "");
                    vkMyDoc.Save();
                    vkMyDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);
                }

                //
                //  Replace in all folders
                //
                string[] folders = Directory.GetDirectories(originalFolder);
                foreach (string folder in folders)
                {
                    ReplaceStringInAllFiles(folder,
                                            tagList,
                                            vkWordApp);
                }
            }
        }
示例#2
0
        // ---------------------------------------------
        //             Copy Documents
        // ---------------------------------------------
        public static object CopyDocument(
            object fromFileName,
            object destinationFileName,
            List <WordDocumentTasks.TagStructure> tag
            )
        {
            Word.Application vkWordApp =
                new Word.Application();

            object saveFile = destinationFileName;

            object vkReadOnly = false;
            object vkVisible  = true;
            object vkFalse    = false;
            object vkTrue     = true;
            object vkDynamic  = 2;

            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's copy the document
            File.Copy(fromFileName.ToString(), destinationFileName.ToString(), true);

            // Let's open the DESTINATION document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref destinationFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            foreach (var t in tag)
            {
                FindAndReplace(t.Tag, t.TagValue, 1, vkWordApp, vkMyDoc);
            }

            vkMyDoc.Save();

            // close the new document
            vkMyDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);

            // close word application
            vkWordApp.Quit(ref vkFalse, ref vkMissing, ref vkMissing);

            return(saveFile);
        }
示例#3
0
 public void Save( )
 {
     oDoc.Save();
 }
示例#4
0
文件: Form1.cs 项目: XoMen/Goshatag
        private void btn_start_Click(object sender, EventArgs e)
        {
            //check if template exists
            if (ofd_template.FileName.Length == 0)
            {
                MessageBox.Show("first choose a doc word");
                return;
            }

            // check empty table
            if (dgv_data.Rows.Count == 0)
            {
                MessageBox.Show("table is empty"); return;
            }

            //check row contents
            int base_vle = -1;

            for (int i = 0; i < dgv_data.Rows.Count - 1; i++)
            {
                if (base_vle == -1)
                {
                    base_vle = Convert.ToInt32(dgv_data.Rows[i].Cells[2].Value);
                }
                else
                {
                    int this_vle = Convert.ToInt32(dgv_data.Rows[i].Cells[2].Value);;
                    if (this_vle != base_vle)
                    {
                        MessageBox.Show("content rows are not same!");
                        return;
                    }
                }
            }

            Directory.CreateDirectory("export/");
            if (Directory.GetFiles("export/").Length > 0)
            {
                MessageBox.Show("first empty export folder"); return;
            }

            progressBar1.Maximum = base_vle;
            progressBar1.Value   = 0;
            for (int i = 0; i < base_vle; i++)
            {
                progressBar1.Value = i + 1;

                Word.Application wordApp = new Word.Application();

                Word.Document aDoc = null;


                wordApp.Visible = false;


                File.Copy(ofd_template.FileName, "export/" + i + ".docx");

                aDoc = wordApp.Documents.Open(System.Windows.Forms.Application.StartupPath + "/export/" + i + ".docx");


                aDoc.Activate();


                //  Call FindAndReplace()function for each change
                for (int ii = 0; ii < dgv_data.RowCount - 1; ii++)
                {
                    string   this_tag = dgv_data.Rows[ii].Cells[0].Value.ToString();
                    string[] contents = dic_data[this_tag];

                    this.FindAndReplace(wordApp, this_tag, contents[i]);
                }


                aDoc.Save();
                aDoc.Close();
            }
            MessageBox.Show("done!");
        }
示例#5
0
        // ---------------------------------------------
        //             Copy Documents
        // ---------------------------------------------
        public static object CopyDocumentReplaceContents(
            object fromFileName,
            object destinationFileName,
            List <WordDocumentTasks.TagStructure> tag
            )
        {
            Word.Application vkWordApp =
                new Word.Application();

            object saveFile = destinationFileName;

            object vkReadOnly = false;
            object vkVisible  = true;
            object vkFalse    = false;
            object vkTrue     = true;
            object vkDynamic  = 2;


            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's open the document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref fromFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            // Let's create a new document
            Word.Document vkNewDoc = vkWordApp.Documents.Add(
                ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkVisible);

            // Select and Copy from the original document
            vkMyDoc.Select();
            vkWordApp.Selection.Copy();

            // Paste into new document as unformatted text
            vkNewDoc.Select();
            vkWordApp.Selection.PasteSpecial(ref vkMissing, ref vkFalse,
                                             ref vkMissing, ref vkFalse, ref vkDynamic,
                                             ref vkMissing, ref vkMissing);

            // Save the new document
            vkNewDoc.SaveAs(ref saveFile, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing);


            // Copy elements
            // FromString => toString
            //
            // underdevelopment
            //
            vkNewDoc.Select();

            foreach (var t in tag)
            {
                FindAndReplace(t.Tag, t.TagValue, 1, vkWordApp, vkMyDoc);
            }

            vkNewDoc.Save();

            // close the new document
            vkNewDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);

            // close the original document
            vkMyDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);

            // close word application
            vkWordApp.Quit(ref vkFalse, ref vkMissing, ref vkMissing);

            return(saveFile);
        }