Windows Develop Bookmark and Share   
 index > Windows Forms General > Can't Find/replace text in a Textbox in word document. Please helps.
 

Can't Find/replace text in a Textbox in word document. Please helps.

Hi Guys,
is there anyway we can find/replace text in a textbox in a word document, a function that will find any text in any sub category, header, footer, textboxt, table etc? I tried the function below but i can only find the text in the main document. all text in the textbox can't be find. I know that Textbox is in shapes (or i think it is since the only place to add a textbox is in the document.shape.addtextbox()).

i tried many method including looping through each shape and use my_shape.application.selection.find method but still no luck. Please help when you get a chance.

any comment/suggestion/hint would be greatly appreciated.

Thanks again in advance.



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 oAll = Word.WdReplace.wdReplaceAll;
object oTrue = true;
object oFalse = false;
object oMissing = System.Reflection.Missing.Value;
object oFile_Loc = sFile_Loc;
object oFindWrap = Word.WdFindWrap.wdFindContinue;

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)
{
/*
object oRange_Index = 0;
foreach (Word.Shape My_Shape in my_Document.Shapes)
{
Word.ShapeRange My_Shape_Range = my_Document.Shapes.Range(ref oRange_Index);
My_Shape_Range.f
}
*/


foreach (Word.Range My_Story in my_Document.StoryRanges)
{
/*
foreach (Word.Shape My_Shape in my_Document.Shapes)
{
My_Shape.Application.Selection.Find.Replacement.ClearFormatting();
My_Shape.Application.Selection.Find.ClearFormatting();
object oFindText = li_One.Value;
object oReplaceText = li_One.Text;
My_Shape.Application.Selection.Find.Execute(ref oFindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oFindWrap, ref oTrue, ref oReplaceText, ref oAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
*/


My_Story.Find.Replacement.ClearFormatting();
My_Story.Find.ClearFormatting();
object oFindText = li_One.Value;
object oReplaceText = li_One.Text;
My_Story.Find.Execute(ref oFindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oFindWrap, 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);
}
}
Romeomussluv  Monday, September 21, 2009 10:50 PM

This code will change the text in textboxes.

Hope it's what you are looking for

            foreach (Shape My_Shape in doc.Shapes)
            {
                My_Shape.TextFrame.TextRange.Text = My_Shape.TextFrame.TextRange.Text.Replace("foo", "alternate foo");

            }
Tamer Oz  Tuesday, September 22, 2009 6:03 AM

This code will change the text in textboxes.

Hope it's what you are looking for

            foreach (Shape My_Shape in doc.Shapes)
            {
                My_Shape.TextFrame.TextRange.Text = My_Shape.TextFrame.TextRange.Text.Replace("foo", "alternate foo");

            }
Tamer Oz  Tuesday, September 22, 2009 6:03 AM

You can use google to search for other answers

Custom Search

More Threads

• Using .resx for language translation
• OpenFileDialog for Folders only
• Printing help needed ?
• Error Creating Window Handle
• Problem with ToolStripControlHost
• Question on GetWindowPlacement()
• How do I give a TextBox focus when a Form opens?
• ListView Help
• Not really understanding CR
• ListBox Drag-n-Drop Change Background Color of just one item