Beispiel #1
0
        private Boolean ExecuteReplace(Microsoft.Office.Interop.Word.
                                       Find find, Object replaceOption)
        {
            Object findText          = Type.Missing;
            Object matchCase         = Type.Missing;
            Object matchWholeWord    = Type.Missing;
            Object matchWildcards    = Type.Missing;
            Object matchSoundsLike   = Type.Missing;
            Object matchAllWordForms = Type.Missing;
            Object forward           = Type.Missing;
            Object wrap            = Type.Missing;
            Object format          = Type.Missing;
            Object replaceWith     = Type.Missing;
            Object replace         = replaceOption;
            Object matchKashida    = Type.Missing;
            Object matchDiacritics = Type.Missing;
            Object matchAlefHamza  = Type.Missing;
            Object matchControl    = Type.Missing;

            return(find.Execute(ref findText, ref matchCase,
                                ref matchWholeWord, ref matchWildcards, ref matchSoundsLike,
                                ref matchAllWordForms, ref forward, ref wrap, ref format,
                                ref replaceWith, ref replace, ref matchKashida,
                                ref matchDiacritics, ref matchAlefHamza, ref matchControl));
        }
Beispiel #2
0
        private bool ExecuteReplace(Word.Find find, object replaceOption)
        {
            object findText          = Type.Missing;
            object matchCase         = Type.Missing;
            object matchWholeWord    = Type.Missing;
            object matchWildcards    = Type.Missing;
            object matchSoundsLike   = Type.Missing;
            object matchAllWordForms = Type.Missing;
            object forward           = Type.Missing;
            object wrap            = Type.Missing;
            object format          = Type.Missing;
            object replaceWith     = Type.Missing;
            object matchKashida    = Type.Missing;
            object matchDiacritics = Type.Missing;
            object matchAlefHamza  = Type.Missing;
            object matchControl    = Type.Missing;

            return(find.Execute(
                       ref findText, ref matchCase,
                       ref matchWholeWord, ref matchWildcards,
                       ref matchSoundsLike, ref matchAllWordForms,
                       ref forward, ref wrap,
                       ref format, ref replaceWith,
                       ref replaceOption, ref matchKashida,
                       ref matchDiacritics, ref matchAlefHamza, ref matchControl
                       ));
        }
Beispiel #3
0
 public bool isExist()
 {
     wdFind = wdApp.Selection.Find;
     wdFind.ClearFormatting();
     wdFind.Text = "[" + beginOfSearchingMask + "]*[" + endOfSearchingMask + "]";
     wdFind.Replacement.ClearFormatting();
     wdFind.MatchWildcards = true;
     return(wdFind.Execute());
 }
 private void btn_Begin_Click(object sender, EventArgs e)
 {
     btn_Begin.Enabled = false;                                       //停用替换按钮
     ThreadPool.QueueUserWorkItem(                                    //开始线程池
         (o) =>                                                       //使用Lambda表达式
     {
         G_WordApplication =                                          //创建Word应用程序对象
                             new Microsoft.Office.Interop.Word.Application();
         object P_FilePath        = G_OpenFileDialog.FileName;        //创建Object对象
         Word.Document P_Document = G_WordApplication.Documents.Open( //打开Word文档
             ref P_FilePath, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing);
         Word.Range P_Range =                            //得到文档范围
                              P_Document.Range(ref G_Missing, ref G_Missing);
         Word.Find P_Find =                              //得到Find对象
                            P_Range.Find;
         this.Invoke(                                    //在窗体线程中执行
             (MethodInvoker)(() =>                       //使用Lambda表达式
         {
             P_Find.Text = txt_Find.Text;                //设置查找的文本
             P_Find.Replacement.Text = txt_Replace.Text; //设置替换的文本
         }));
         object P_Replace = Word.WdReplace.wdReplaceAll; //定义替换方式对象
         bool P_bl        = P_Find.Execute(              //开始替换
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing, ref P_Replace,
             ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing);
         G_WordApplication.Documents.Save(            //保存文档
             ref G_Missing, ref G_Missing);
         ((Word._Document)P_Document).Close(          //关闭文档
             ref G_Missing, ref G_Missing, ref G_Missing);
         ((Word._Application)G_WordApplication).Quit( //退出Word应用程序
             ref G_Missing, ref G_Missing, ref G_Missing);
         this.Invoke(                                 //在窗体线程中执行
             (MethodInvoker)(() =>                    //使用Lambda表达式
         {
             if (P_bl)                                //查看是否找到并替换
             {
                 MessageBox.Show(                     //弹出消息对话框
                     "找到字符串并替换", "提示!");
                 btn_Display.Enabled = true;          //启用显示文件按钮
             }
             else
             {
                 MessageBox.Show(        //弹出消息对话框
                     "没有找到字符串", "提示!");
             }
             btn_Begin.Enabled = true;        //启用开始替换按钮
         }));
     });
 }
 private void btn_Begin_Click(object sender, EventArgs e)
 {
     btn_Begin.Enabled = false;                                       //停用替換按鈕
     ThreadPool.QueueUserWorkItem(                                    //開始線程池
         (o) =>                                                       //使用Lambda表達式
     {
         G_WordApplication =                                          //建立Word應用程式物件
                             new Microsoft.Office.Interop.Word.Application();
         object P_FilePath        = G_OpenFileDialog.FileName;        //建立Object物件
         Word.Document P_Document = G_WordApplication.Documents.Open( //打開Word文件檔
             ref P_FilePath, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing);
         Word.Range P_Range =                            //得到文件檔範圍
                              P_Document.Range(ref G_Missing, ref G_Missing);
         Word.Find P_Find =                              //得到Find物件
                            P_Range.Find;
         this.Invoke(                                    //在視窗線程中執行
             (MethodInvoker)(() =>                       //使用Lambda表達式
         {
             P_Find.Text = txt_Find.Text;                //設定搜尋的文字
             P_Find.Replacement.Text = txt_Replace.Text; //設定替換的文字
         }));
         object P_Replace = Word.WdReplace.wdReplaceAll; //定義替換方式物件
         bool P_bl        = P_Find.Execute(              //開始替換
             ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
             ref G_Missing, ref G_Missing, ref G_Missing, ref P_Replace,
             ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing);
         G_WordApplication.Documents.Save(            //儲存文件檔
             ref G_Missing, ref G_Missing);
         ((Word._Document)P_Document).Close(          //關閉文件檔
             ref G_Missing, ref G_Missing, ref G_Missing);
         ((Word._Application)G_WordApplication).Quit( //退出Word應用程式
             ref G_Missing, ref G_Missing, ref G_Missing);
         this.Invoke(                                 //在視窗線程中執行
             (MethodInvoker)(() =>                    //使用Lambda表達式
         {
             if (P_bl)                                //查看是否找到並替換
             {
                 MessageBox.Show(                     //彈出消息對話框
                     "找到字串並替換", "提示!");
                 btn_Display.Enabled = true;          //啟用顯示文件按鈕
             }
             else
             {
                 MessageBox.Show(        //彈出消息對話框
                     "沒有找到字串", "提示!");
             }
             btn_Begin.Enabled = true;        //啟用開始替換按鈕
         }));
     });
 }
Beispiel #6
0
        private void run_replace(WORD.Find find, string src, string dest)
        {
            object objMissing = Type.Missing;

            find.Text             = src;
            find.Replacement.Text = dest;
            find.Execute(ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                         ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref myReplace,
                         ref objMissing, ref objMissing, ref objMissing, ref objMissing);
        }
Beispiel #7
0
        /// <summary>
        /// Search and replace all occurences of a string in a active document.
        /// </summary>
        /// <param name="app">Word application instance.</param>
        /// <param name="find">String to search for.</param>
        /// <param name="replaceWith">String to replace the searched value with.</param>
        /// <param name="textColor">A color to give to the replaced text.</param>
        public void searchAndReplace(Document wordDocument, string find, string replaceWith, Color textColor = new Color(), bool searchInLastTextBox = false)
        {
            if (!searchInLastTextBox)
            {
                //Bug fix for searching and replacing it with long strings. Fix it by calling this function recursifly.
                string replaceWithPart = "";
                if (replaceWith.Length > searchReplaceLenght)
                {
                    replaceWithPart = replaceWith.Substring(searchReplaceLenght);
                    replaceWith     = replaceWith.Substring(0, searchReplaceLenght) + "<rep>";
                }

                Microsoft.Office.Interop.Word.Find findObject = this.app.Selection.Find;
                findObject.ClearFormatting();
                findObject.Text = find;
                findObject.Replacement.ClearFormatting();
                findObject.Replacement.Text = replaceWith;

                //Set text color if it is given.
                if (textColor != new Color())
                {
                    findObject.Replacement.Font.Color = (Microsoft.Office.Interop.Word.WdColor)(textColor.R + 0x100 * textColor.G + 0x10000 * textColor.B);
                }
                object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                                   ref missing, ref missing, ref missing, ref missing, ref missing,
                                   ref replaceAll, ref missing, ref missing, ref missing, ref missing);

                //Call searchAndReplace recursively if we have a long string.
                if (replaceWithPart.Length > 0)
                {
                    searchAndReplace(wordDocument, "<rep>", replaceWithPart, textColor, searchInLastTextBox);
                }
            }
            else
            {
                //Also check shapes
                Microsoft.Office.Interop.Word.Shape shape = wordDocument.Shapes[wordDocument.Shapes.Count];

                //Is it a textbox.
                if (shape.Type == MsoShapeType.msoTextBox)
                {
                    Microsoft.Office.Interop.Word.Find findObject = shape.TextFrame.TextRange.Find;
                    findObject.ClearFormatting();
                    findObject.Text = find;
                    findObject.Replacement.ClearFormatting();
                    findObject.Replacement.Text = replaceWith;

                    findObject.Execute((object)find,
                                       ref missing, ref missing, ref missing, ref missing, ref missing, ref paramFalse,
                                       ref missing, ref missing, (object)replaceWith, ref missing, ref missing, ref missing,
                                       ref missing, ref missing);
                }
            }
        }
Beispiel #8
0
        private void FindReplace(string FieldText, string FieldValue)
        {
            Word.Find findObject = oWord.Selection.Find;

            findObject.Text = FieldText;
            findObject.ClearFormatting();


            switch (FieldText)
            {
            case "Organization":
                findObject.Text = "<Organization>";
                break;

            case "BCRS":
                findObject.Text = "<T_SCORES_BCRS%>";
                break;

            case "AC":
                findObject.Text = "<T_SCORES_AC>";
                break;

            case "AAP":
                findObject.Text = "<T_SCORES_AAP>";
                break;

            case "IVC":
                findObject.Text = "<" + "T_SCORES_IVC" + ">";
                break;

            case "SI":
                findObject.Text = "<" + "T_SCORES_SI" + ">";
                break;

            case "DC":
                findObject.Text = "<" + "T_SCORES_DC" + ">";
                break;

            case "MATRIX LABEL":
                findObject.Text = "<" + "T_SCORES_MATLABEL" + ">";
                break;

            default:
                System.Console.WriteLine(FieldText);
                break;
            }

            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = FieldValue;
            object replace = Word.WdReplace.wdReplaceAll;

            findObject.Execute(ref OMissing, ref OMissing, ref OMissing, ref OMissing, ref OMissing,
                               ref OMissing, ref OMissing, ref OMissing, ref OMissing, ref OMissing,
                               ref replace, ref OMissing, ref OMissing, ref OMissing, ref OMissing);
        }
Beispiel #9
0
        /*Function which finds all occurences of a particular word*/
        public Boolean ExecuteFind(MSword.Find find) {
            object missing = System.Type.Missing;
            object wholeword = true;
            object MatchCase = true;

            return find.Execute(ref missing, ref MatchCase, ref wholeword,
              ref missing, ref missing, ref missing,
              ref missing, ref missing, ref missing, ref missing, ref missing,
              ref missing, ref missing, ref missing,
              ref missing);
        }
Beispiel #10
0
        internal bool Process(Dictionary <string, string> items)
        {
            Word.Application app = null;
            try
            {
                app = new Word.Application();
                Object file = _fileInfo.FullName;

                Object missing = Type.Missing;

                app.Documents.Open(file);

                foreach (var item in items)
                {
                    Word.Find find = app.Selection.Find;
                    find.Text             = item.Key;
                    find.Replacement.Text = item.Value;

                    Object wrap    = Word.WdFindWrap.wdFindContinue;
                    Object replace = Word.WdReplace.wdReplaceAll;

                    find.Execute(FindText: Type.Missing,
                                 MatchCase: false,
                                 MatchWholeWord: false,
                                 MatchWildcards: false,
                                 MatchSoundsLike: missing,
                                 MatchAllWordForms: false,
                                 Forward: true,
                                 Wrap: wrap,
                                 Format: false,
                                 ReplaceWith: missing, Replace: replace);
                }

                Object newFileName = Path.Combine(_fileInfo.DirectoryName, DateTime.Now.ToString("yyyyMMdd HHmmss") + _fileInfo.Name);
                app.ActiveDocument.SaveAs2(newFileName);
                app.ActiveDocument.Close();



                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (app != null)
                {
                    app.Quit();
                }
            }
            return(false);
        }
Beispiel #11
0
        public void FindReplace(string str_old, string str_new)
        {
            Word.Find find = app.Selection.Find;

            find.Text             = str_old; // текст поиска
            find.Replacement.Text = str_new; // текст замены

            find.Execute(FindText: Type.Missing, MatchCase: false, MatchWholeWord: false, MatchWildcards: false,
                         MatchSoundsLike: missing, MatchAllWordForms: false, Forward: true, Wrap: Word.WdFindWrap.wdFindContinue,
                         Format: false, ReplaceWith: missing, Replace: Word.WdReplace.wdReplaceAll);
        }
Beispiel #12
0
        /** Searches through the active document for a string. Returns true if found; else, false. **/
        private Boolean Find(String text, Boolean matchCase)
        {
            Word.Find thisFind = doc.Content.Find;
            thisFind.Text           = text;
            thisFind.Format         = false;
            thisFind.Wrap           = Word.WdFindWrap.wdFindContinue;
            thisFind.MatchCase      = matchCase;
            thisFind.MatchWholeWord = true;

            return(thisFind.Execute());
        }
        private void createCopy()
        {
            object missing     = System.Reflection.Missing.Value;
            string newFilename = "";
            string timeStamp   = String.Format("{0:f}", DateTime.Now);

            timeStamp = timeStamp.Replace(':', ' ');
            string destinationFolder = @"C:\VSProject\resumeGenerator\" + timeStamp;

            try
            {
                if (Directory.Exists(destinationFolder))
                {
                    destinationFolder += " 2";
                }
                DirectoryInfo di         = Directory.CreateDirectory(destinationFolder);
                string        searchText = "Address";
                foreach (KeyValuePair <string, string> replaceAddress in address)
                {
                    newFilename = destinationFolder + @"\" + replaceAddress.Key + ".pdf";

                    //replacing Address
                    Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
                    wordDocument = application.Documents.Open(this.filename, ref missing, false, 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);

                    Microsoft.Office.Interop.Word.Find findObject = application.Selection.Find;
                    findObject.ClearFormatting();
                    findObject.Text = searchText;
                    findObject.Replacement.ClearFormatting();
                    findObject.Replacement.Text = replaceAddress.Value;
                    object replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;
                    findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                                       ref missing, ref missing, ref missing, ref missing, ref missing,
                                       ref replace, ref missing, ref missing, ref missing, ref missing);
                    //done replacing

                    if (File.Exists(newFilename))
                    {
                        File.Delete(newFilename);
                    }

                    wordDocument.ExportAsFixedFormat(newFilename, WdExportFormat.wdExportFormatPDF);
                    ((_Document)wordDocument).Close();
                    ((_Application)application).Quit();

                    searchText = replaceAddress.Value;
                }
            }
            catch (Exception e)
            {
                throw new System.ArgumentException("Error in generating copies " + e.Message);
            }
        }
Beispiel #14
0
        public void SearchReplace(Word.Find findObject, string fintpattern, string raplaceSrting)
        {
            findObject.ClearFormatting();
            findObject.Text = fintpattern;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = raplaceSrting;
            object replaceAll = Word.WdReplace.wdReplaceAll;

            findObject.Execute(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                               Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                               ref replaceAll, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }
Beispiel #15
0
        private void FindAndReplace(Word.Application app, object findText, object replaceWithText)
        {
            object missing = Missing.Value;

            Word.Find findObject = app.ActiveWindow.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = findText as String;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = replaceWithText as String;

            object replaceAll = Word.WdReplace.wdReplaceAll;

            findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref replaceAll, ref missing, ref missing, ref missing, ref missing);

            /*Word.Find fnd = app.ActiveWindow.Selection.Find;
             *
             * fnd.ClearFormatting();
             * fnd.Replacement.ClearFormatting();
             * fnd.Forward = true;
             * fnd.Wrap = Word.WdFindWrap.wdFindContinue;
             *
             * fnd.Text = findText as String;
             * fnd.Replacement.Text = replaceWithText as String;
             *
             * fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);*/

            /*object matchCase = true;
             * object matchWholeWord = true;
             * object matchWillCards = false;
             * object matchSoundLike = false;
             * object nmatchAllForms = false;
             * object forward = true;
             * object format = false;
             * object matchKashida = false;
             * object matchDiactitics = false;
             * object matchAleftHamza = false;
             * object matchControl = false;
             * object read_Only = false;
             * object visible = false;
             * object replace = 2;
             * object wrap = 1;
             *
             * app.Selection.Find.Execute(ref findText, ref matchCase,
             *                          ref matchWholeWord, ref matchWillCards,
             *                          ref matchSoundLike, ref nmatchAllForms,
             *                          ref forward, ref wrap, ref format,
             *                          ref replaceWithText, ref replace, ref matchKashida,
             *                          ref matchDiactitics, ref matchAleftHamza,
             *                          ref matchControl);*/
        }
Beispiel #16
0
        public void SearchReplace(string strToFind, string replaceStr)
        {
            Word.Find findObject = _application.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = strToFind;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = replaceStr;

            object replaceAll = Word.WdReplace.wdReplaceOne;

            findObject.Execute(ref _missingObj, ref _missingObj, ref _missingObj, ref _missingObj, ref _missingObj,
                               ref _missingObj, ref _missingObj, ref _missingObj, ref _missingObj, ref _missingObj,
                               ref replaceAll, ref _missingObj, ref _missingObj, ref _missingObj, ref _missingObj);
        }
 private void FindAndReplace(Word.Application wordApp, object findText, object replaceText)
 {
     try
     {
         Word.Find myFind       = wordApp.Selection.Find;
         object    typeMissing  = System.Reflection.Missing.Value;
         object    wdReplaceAll = Word.WdReplace.wdReplaceAll;
         myFind.Execute(ref findText, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref replaceText, ref wdReplaceAll, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Beispiel #18
0
    public void replaceMark(string textForSearching, string textForReplacement)
    {
        wdFind = wdApp.Selection.Find;
        wdFind.ClearFormatting();
        wdFind.Text = textForSearching;
        wdFind.Replacement.ClearFormatting();
        wdFind.Replacement.Text = textForReplacement;

        object replaceAll = Word.WdReplace.wdReplaceAll;

        wdFind.Execute(ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
                       ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
                       ref replaceAll, ref missingObj, ref missingObj, ref missingObj, ref missingObj);
    }
Beispiel #19
0
        //</Snippet71>


        //---------------------------------------------------------------------
        //<Snippet75>
        private void SearchReplace()
        {
            Word.Find findObject = Application.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = "find me";
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = "Found";

            object replaceAll = Word.WdReplace.wdReplaceAll;

            findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref replaceAll, ref missing, ref missing, ref missing, ref missing);
        }
        void myReplace(object missing, Word._Application oWord, string oldString, string newString)
        {
            Word.Find findObject = oWord.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = oldString;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = newString;

            object replaceAll = Word.WdReplace.wdReplaceAll;

            findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref replaceAll, ref missing, ref missing, ref missing, ref missing);
        }
Beispiel #21
0
        /// <summary>
        /// Find and mark all instances of a string in a specific StoryRange.
        /// </summary>
        /// <param name="StoryRange">A Range specifying the story range to search.</param>
        /// <returns>True if a hit was found, False otherwise.</returns>
        private int FindAndMarkInStory(Word.Range StoryRange)
        {
            object    CollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
            RangeData LastHit     = new RangeData();
            int       HitCount    = 0;
            object    Missing     = Type.Missing;
            object    FindText    = textBox.Text;

            //text boxes in headers/footers aren't in the text box story
            if ((int)StoryRange.StoryType > 5 && StoryRange.ShapeRange.Count > 0)
            {
                foreach (Word.Shape Shape in StoryRange.ShapeRange)
                {
                    Word.Range ShapeRange = RedactCommon.RangeFromShape(Shape);
                    if (ShapeRange != null)
                    {
                        HitCount += FindAndMarkInStory(ShapeRange);
                    }
                }
            }

            Word.Find FindScope = StoryRange.Find;

            //set search options
            FindScope.IgnorePunct       = checkBoxIgnorePunct.Checked;
            FindScope.IgnoreSpace       = checkBoxIgnoreWhitespace.Checked;
            FindScope.MatchAllWordForms = checkBoxWordForms.Checked;
            FindScope.MatchCase         = checkBoxMatchCase.Checked;
            FindScope.MatchPrefix       = checkBoxMatchPrefix.Checked;
            FindScope.MatchSoundsLike   = checkBoxSoundsLike.Checked;
            FindScope.MatchSuffix       = checkBoxMatchSuffix.Checked;
            FindScope.MatchWholeWord    = checkBoxWholeWord.Checked;
            FindScope.MatchWildcards    = checkBoxWildcards.Checked;

            while (FindScope.Execute(ref FindText, 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) && (StoryRange.Start != LastHit.Start || StoryRange.End != LastHit.End))
            {
                HitCount++;
                LastHit = new RangeData(StoryRange.Start, StoryRange.End);
                StoryRange.Font.Shading.BackgroundPatternColor = (Word.WdColor)ShadingColor;
                StoryRange.Collapse(ref CollapseEnd);
            }

            //check in any subsequent stories
            if (StoryRange.NextStoryRange != null)
            {
                HitCount += FindAndMarkInStory(StoryRange.NextStoryRange);
            }

            return(HitCount);
        }
Beispiel #22
0
        private void button4_Click(object sender, EventArgs e)
        {
            string serial = txtserial.Text;
            string cid    = txtcomputer_id.Text;

            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application {
                Visible = false
            };

            Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(filePath, ReadOnly: false, Visible: true);

            aDoc.Activate();

            Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find;

            fnd.ClearFormatting();
            fnd.Replacement.ClearFormatting();
            fnd.Forward = true;

            fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;

            fnd.Text             = "#cid#";
            fnd.Replacement.Text = cid;
            fnd.Execute(Replace: WdReplace.wdReplaceAll);
            fnd.Text             = "#serial#";
            fnd.Replacement.Text = serial;
            fnd.Execute(Replace: WdReplace.wdReplaceAll);
            fnd.Text             = "#company#";
            fnd.Replacement.Text = txtcompany.Text;
            fnd.Execute(Replace: WdReplace.wdReplaceAll);
            string date = DateTime.Now.ToString("MMMMddyyyy");

            aDoc.SaveAs2(savepath + "\\" + "deactivation form " + companydeact + date, WdSaveFormat.wdFormatPDF);
            createandsendpath = savepath + "\\" + "deactivation form " + companydeact + date + ".pdf";
            aDoc.Close(SaveChanges: false);
            //Microsoft.Office.Interop.Word.Document aDoc1 = wordApp.Documents.Open(savepath + "\\" + "deactivation form " + companydeact, ReadOnly: true, Visible: true);
        }
Beispiel #23
0
 void zamena(Object missing, Object wrap, Object replace, Word.Find find, string from, string naChtoMenyaem)
 {
     find.Text             = from;
     find.Replacement.Text = naChtoMenyaem.Replace("(", "").Replace(")", "");
     find.Execute(FindText: Type.Missing,
                  MatchCase: false,
                  MatchWholeWord: false,
                  MatchWildcards: false,
                  MatchSoundsLike: missing,
                  MatchAllWordForms: false,
                  Forward: true,
                  Wrap: wrap,
                  Format: false,
                  ReplaceWith: missing, Replace: replace);
 }
Beispiel #24
0
        public void ReplaceWord(Word.Application wordApp, string ReplaceText, string ReplacementText)
        {
            Object missing = Type.Missing;

            Word.Find findObject = wordApp.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = ReplaceText;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = ReplacementText;
            object replaceAll = Word.WdReplace.wdReplaceAll;

            findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref replaceAll, ref missing, ref missing, ref missing, ref missing);
        }
Beispiel #25
0
        public static void FindAndReplace(Word.Application app, string OldStr, string NewStr)
        {
            Word.Find findObject = app.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = OldStr;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = NewStr;

            object missing    = System.Type.Missing;
            object replaceAll = Word.WdReplace.wdReplaceAll;

            findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref replaceAll, ref missing, ref missing, ref missing, ref missing);
        }
Beispiel #26
0
        private void changeLetters(int flag)
        {
            System.Diagnostics.Debug.Assert(flag != 2 || flag != 1, "Nedozvoljena vrijednost flag-a");
            string[] array1 = null;
            string[] array2 = null;
            if (flag == 1)
            {
                array1 = latinica;
                array2 = cirilica;
            }
            else if (flag == 2)
            {
                array1 = cirilica;
                array2 = latinica;
            }

            Word.Document document = this.Application.ActiveDocument;
            document.Application.ScreenUpdating = false;
            Word.Range selectedPart = null;
            if (justSelected == false)
            {
                selectedPart = document.Range(document.Content.Start, document.Content.End);
            }
            else
            {
                selectedPart = document.Range(this.Application.Selection.Start, this.Application.Selection.End);
            }

            for (int i = 0; i < array1.Length; i++)
            {
                Word.Find findObject = selectedPart.Find;

                findObject.ClearFormatting();
                findObject.Text = array1[i];
                findObject.Replacement.ClearFormatting();
                findObject.Replacement.Text = array2[i];

                object replaceAll     = Word.WdReplace.wdReplaceAll;
                object matchCase      = false;
                object matchWholeWord = false;
                findObject.Execute(ref missing, matchCase, matchWholeWord, ref missing, ref missing,
                                   ref missing, ref missing, ref missing, ref missing, ref missing,
                                   ref replaceAll, ref missing, ref missing, ref missing, ref missing);
            }

            document.Application.ScreenRefresh();
            document.Application.ScreenUpdating = true;
        }
Beispiel #27
0
 /// <summary>
 /// Заменяет текст на заданный.
 /// </summary>
 /// <param name="findText">Текст который нужно заменить.</param>
 /// <param name="newText">Текст на который будет заменен.</param>
 public void Replace(string findText, string newText)
 {
     Find.Text             = findText;
     Find.Replacement.Text = newText;
     Find.Execute(FindText: Missing,
                  MatchCase: false,
                  MatchWholeWord: false,
                  MatchWildcards: false,
                  MatchSoundsLike: Missing,
                  MatchAllWordForms: false,
                  Forward: true,
                  Wrap: Wrap,
                  Format: false,
                  ReplaceWith: Missing,
                  Replace: WdReplace);
 }
Beispiel #28
0
        //This is different. It supports replacing with insert of \r\n.
        private void SearchAndType(string SearchFor, string ToType, word.Application wordApp)
        {
            word.Find findObj = wordApp.Selection.Find;
            findObj.ClearFormatting();
            findObj.Text = SearchFor;
            findObj.Replacement.ClearFormatting();
            findObj.Replacement.Text = "";


            object replace = word.WdReplace.wdReplaceOne;

            findObj.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing, ref missing,
                            ref replace, ref missing, ref missing, ref missing, ref missing);

            wordApp.Selection.TypeText(ToType);
        }
Beispiel #29
0
        public string FindTextByStyleForCurrentDocument(string style)
        {
            WordOM.Find find = _document.Application.Selection.Find;

            int r = _document.Application.Selection.Move();


            //error handling needed.
            // ReSharper disable UseIndexedProperty
            find.set_Style(_document.Styles[style]);
            // ReSharper restore UseIndexedProperty
            find.Text           = String.Empty;
            find.Forward        = false;
            find.MatchWildcards = true;
            find.Execute();
            return(_document.Application.Selection.Text);
        }
Beispiel #30
0
        /// <summary>
        /// Finds and replaces a string of text with another string of text in the given word document
        /// </summary>
        private void ReplaceTextWithText(string textToReplace, string replacementText, Word.Application wordApp)
        {
            object replaceAll = Word.WdReplace.wdReplaceAll;

            Word.Find findObject = wordApp.Selection.Find;
            try {
                //Attempt to find and replace the given text to replace with the given replacement text in one command.
                findObject.Execute(textToReplace, true, true, false, false, false, false, Word.WdFindWrap.wdFindAsk, false, replacementText, replaceAll, false, false, false, false);
            }
            catch (Exception e) {
                MessageBox.Show(new Form {
                    TopMost = true
                }, e.Message + "at command: " + textToReplace);
                wordApp.Quit();
                Environment.Exit(1);
            }
        }