Beispiel #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);
                }
            }
        }
Beispiel #2
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);
        }
Beispiel #3
0
        /// <summary>
        /// Get source document. Open a FileDialog window for user to select single/multiple files for
        /// parsing.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butSourceDocument_Click(object sender, System.EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                object fileName = openFileDialog.FileName;
                object saveFile = fileName + "_Vk.doc";


                object vk_read_only = false;
                object vk_visible   = true;
                object vk_false     = false;
                object vk_true      = true;
                object vk_dynamic   = 2;


                object vk_missing = System.Reflection.Missing.Value;

                // Let make the word application visible
                vk_word_app.Visible = true;
                vk_word_app.Activate();

                // Let's open the document
                Word.Document vk_my_doc = vk_word_app.Documents.Open(ref fileName,
                                                                     ref vk_missing, ref vk_read_only, ref vk_missing, ref vk_missing,
                                                                     ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
                                                                     ref vk_missing, ref vk_missing, ref vk_visible);

                // Let's create a new document
                Word.Document vk_new_doc = vk_word_app.Documents.Add(ref vk_missing,
                                                                     ref vk_missing, ref vk_missing, ref vk_visible);

                // Select and Copy from the original document
                vk_my_doc.Select();
                vk_word_app.Selection.Copy();

                // Paste into new document as unformatted text
                vk_new_doc.Select();
                vk_word_app.Selection.PasteSpecial(ref vk_missing, ref vk_false,
                                                   ref vk_missing, ref vk_false, ref vk_dynamic, ref vk_missing, ref vk_missing);

                // close the original document
                vk_my_doc.Close(ref vk_false, ref vk_missing, ref vk_missing);

                // Let try to replace Vahe with VAHE in the new document
                object vk_find    = "^l";
                object vk_replace = " ";
                object vk_num     = 1;

                vk_new_doc.Select();

                WordDocumentTasks.FindAndReplace(
                    "<<Client Name>>",
                    "tEST",
                    vk_num,
                    vk_word_app);

                // Save the new document
                vk_new_doc.SaveAs(ref saveFile, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
                                  ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing);

                // close the new document
                vk_new_doc.Close(ref vk_false, ref vk_missing, ref vk_missing);

/*
 *                              // Let's get the content from the document
 *                              Word.Paragraphs vk_my_doc_p = vk_new_doc.Paragraphs;
 *                              // Count number of paragraphs in the file
 *                              long p_count = vk_my_doc_p.Count;
 *                              // step through the paragraphs
 *                              for( int i=1; i<=p_count; i++ )
 *                              {
 *                                      Word.Paragraph vk_p = vk_my_doc_p.Item( i );
 *                                      Word.Range vk_r = vk_p.Range;
 *                                      string text = vk_r.Text;
 *
 *                                      MessageBox.Show( text );
 *                              }
 */


                // close word application
                vk_word_app.Quit(ref vk_false, ref vk_missing, ref vk_missing);
            }
        }