Windows Develop Bookmark and Share   
 index > Windows Forms General > Word.selection.find.execute() ---- search and replace only work on the first search but not on second, third etc. Plz helps.
 

Word.selection.find.execute() ---- search and replace only work on the first search but not on second, third etc. Plz helps.

Hi all,
I'm developing a program that open a word document and search and then replace the text but im not sure why it only replace the first search/replace but not the second, third etc. i tried many different methods but none worked. i search all over the next and found that find.clearFormatting() should be use before each search but it still doesn't work. the method only found the first search but can't find any search after that.

i debugged and went to each search value and compared in the word document and the values are in there. i switched the listitem order to see if the search value is the problem or not but it is not. no matter which order of the listitem i used, the only one that it replace/found is the first item on the list.

Please help if you know anything about this or if you see any mistakes in the code.

THank you very much in advance. i really appreciate any comment/hint/advice/reply.

Romeo



protected void Replace_Template(string sFile_Loc, ListItem[] li_Replaces)
{
if (File.Exists(sFile_Loc))
{
Word.Application my_Word = new Word.Application();
my_Word.Visible = false;
object oTrue = true;
object oFalse = false;
object oMissing = System.Reflection.Missing.Value;
object oFile_Loc = sFile_Loc;

Word.Document my_Document = (Word.Document)my_Word.Documents.Open(ref oFile_Loc, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing);

foreach (ListItem li_One in li_Replaces)
{
if (li_One != null)
{
my_Word.Selection.Find.Replacement.ClearFormatting();
my_Word.Selection.Find.ClearFormatting();
object oFindText = li_One.Value;
object oReplaceText = li_One.Text;
my_Word.Selection.Find.Execute(ref oFindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oTrue, ref oReplaceText, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
}
my_Document.Save();
my_Document.Close(ref oTrue, ref oMissing, ref oMissing);
my_Word.Quit(ref oTrue, ref oMissing, ref oMissing);
}
}
Romeomussluv  Sunday, September 20, 2009 7:19 AM
Hi,

add

my_Word.Selection.Find.Wrap = Microsoft.Office.Interop.Word.

WdFindWrap.wdFindContinue;

just below

my_Word.Selection.Find.ClearFormatting();

and pass

object

oAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

as parameter to Replace parameter of Execute method instead of oTrue.

Here is the code works fine

        protected void Replace_Template(string sFile_Loc, string[] li_Replaces)
        {
            if (File.Exists(sFile_Loc))
            {
                Microsoft.Office.Interop.Word.Application my_Word = new Microsoft.Office.Interop.Word.Application();
                my_Word.Visible = false;
                object oTrue = true;
                object oAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                object oFalse = false;
                object oMissing = System.Reflection.Missing.Value;
                object oFile_Loc = sFile_Loc;

                Microsoft.Office.Interop.Word.Document my_Document = (Microsoft.Office.Interop.Word.Document)my_Word.Documents.Open(ref oFile_Loc, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                foreach (string li_One in li_Replaces)
                {
                    if (li_One != null)
                    {

                        my_Word.Selection.Find.Replacement.ClearFormatting();
                        my_Word.Selection.Find.ClearFormatting();
                        my_Word.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                        
                        object oFindText = li_One;
                        object oReplaceText = li_One + "1";
                        my_Word.Selection.Find.Execute(ref oFindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oTrue, ref oReplaceText, ref oAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    }
                }
                my_Document.Save();
                my_Document.Close(ref oTrue, ref oMissing, ref oMissing);
                my_Word.Quit(ref oTrue, ref oMissing, ref oMissing);
            }
        }


  • Marked As Answer byRomeomussluv Sunday, September 20, 2009 8:09 AM
  •  
Tamer Oz  Sunday, September 20, 2009 7:37 AM
Hi,

add

my_Word.Selection.Find.Wrap = Microsoft.Office.Interop.Word.

WdFindWrap.wdFindContinue;

just below

my_Word.Selection.Find.ClearFormatting();

and pass

object

oAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

as parameter to Replace parameter of Execute method instead of oTrue.

Here is the code works fine

        protected void Replace_Template(string sFile_Loc, string[] li_Replaces)
        {
            if (File.Exists(sFile_Loc))
            {
                Microsoft.Office.Interop.Word.Application my_Word = new Microsoft.Office.Interop.Word.Application();
                my_Word.Visible = false;
                object oTrue = true;
                object oAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                object oFalse = false;
                object oMissing = System.Reflection.Missing.Value;
                object oFile_Loc = sFile_Loc;

                Microsoft.Office.Interop.Word.Document my_Document = (Microsoft.Office.Interop.Word.Document)my_Word.Documents.Open(ref oFile_Loc, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                foreach (string li_One in li_Replaces)
                {
                    if (li_One != null)
                    {

                        my_Word.Selection.Find.Replacement.ClearFormatting();
                        my_Word.Selection.Find.ClearFormatting();
                        my_Word.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                        
                        object oFindText = li_One;
                        object oReplaceText = li_One + "1";
                        my_Word.Selection.Find.Execute(ref oFindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oTrue, ref oReplaceText, ref oAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    }
                }
                my_Document.Save();
                my_Document.Close(ref oTrue, ref oMissing, ref oMissing);
                my_Word.Quit(ref oTrue, ref oMissing, ref oMissing);
            }
        }


  • Marked As Answer byRomeomussluv Sunday, September 20, 2009 8:09 AM
  •  
Tamer Oz  Sunday, September 20, 2009 7:37 AM
Thank you very much Tamer. I tried to solve this for a couple of hours but no luck until you come.
THanks again buddy.
Romeomussluv  Sunday, September 20, 2009 8:11 AM

You can use google to search for other answers

Custom Search

More Threads

• C# Button Rendercontrol and Click Event
• How to develop custom button control using SendMessage(...)?
• Updating GUI controls from C# Class Library...???
• How to adjust MDI forms size according to screen size...?
• Create an User Interface like Windows Live Mail Desktop
• Need Strategy for Result Set
• Intercepting/Overriding Paste behavior on ComboBox
• webpage aligment behavior in winforms
• locking of combo box
• auto screen resolution for c# window application