none
Find and replace text in textboxes within a word document

    Question

  • Hi there. 

    I have created an interop wrapper when working with office interop (word, outlook etc). 

    One part of this is a find and replace mechanism for auto-populating word documents with data from a database using Range.Find

    This works as intended but I have run into a problem on a new project, the text to find and replace in these documents are inside text boxes and when I ran my code, none of the text in the document was replaced with data dynamically gathered from the database.

    Is there a way to specify that the word interop will scan text in text boxes or I am going to have to remove the textboxes from my word documents?

    Thanks

    Kevin
    Tuesday, January 26, 2010 3:43 PM

Answers

  • I could not get the find and replace to work correctly still when the text was to replace was inside text boxes so I have removed the text boxes so that the text to find is in the main body of the word document and now my code works. It was not mandatory that the text boxes needed to be there I found out later.

    Thanks for your assistance. 
    • Marked as answer by Harry Zhu Tuesday, February 09, 2010 8:33 AM
    Monday, February 01, 2010 11:58 AM

All replies

  • Hi,

    Found here someone who had the exact same problem than yours :
    http://forums.asp.net/t/1501791.aspx

    It was in VB.Net, so I gave it to a CodeTranslator :

    public void FindandReplace(Word.Document doc, string Findtext, string Replacetext)
    {
       
        var myStoryRange = doc.Range();
       
        //First search the main document using the Selection
        {
            myStoryRange.Find.Text = Findtext;
            myStoryRange.Find.Replacement.Text = Replacetext;
            myStoryRange.Find.Forward = true;
            myStoryRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;
            myStoryRange.Find.Format = false;
            myStoryRange.Find.MatchCase = false;
            myStoryRange.Find.MatchWholeWord = false;
            myStoryRange.Find.MatchWildcards = false;
            myStoryRange.Find.MatchSoundsLike = false;
            myStoryRange.Find.MatchAllWordForms = false;
            myStoryRange.Find.Execute(Replace = Word.WdReplace.wdReplaceAll);
        }
       
        //Now search all other stories using Ranges
        foreach (var myStoryRange in doc.StoryRanges) {
            if (myStoryRange.StoryType != Word.WdStoryType.wdMainTextStory) {
                {
                    myStoryRange.Find.Text = Findtext;
                    myStoryRange.Find.Replacement.Text = Replacetext;
                    myStoryRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;
                    myStoryRange.Find.Execute(Replace = Word.WdReplace.wdReplaceAll);
                }
                while ((myStoryRange.NextStoryRange != null)) {
                    myStoryRange = myStoryRange.NextStoryRange;
                    {
                        myStoryRange.Find.Text = Findtext;
                        myStoryRange.Find.Replacement.Text = Replacetext;
                        myStoryRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;
                        myStoryRange.Find.Execute(Replace = Word.WdReplace.wdReplaceAll);
                    }
                }
            }
        }
    }

    Hope this helps,

    Regards,

    Tom
    Thomas Aimonetti - C# - Sharplog Engineering - http://www.sharplog.fr
    • Marked as answer by kevinjp Tuesday, January 26, 2010 5:08 PM
    • Unmarked as answer by kevinjp Wednesday, January 27, 2010 11:35 AM
    Tuesday, January 26, 2010 3:55 PM
  • I have started having problems with this code on another document - only the first textbox is being replaced with the correct text. 

    Here is my code - is there anything obvious I am missing. 

    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = null;
    
                object missing = Type.Missing;
                object sourceDoc = fullPathToSourceDocument;
                object destinationDoc = fullPathToTargetDocument;
                object matchCase = false;
                object matchWholeWord = true;
                object findWrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                object findFormat = true;
                object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                object saveChanges = autoSave;
                object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
                object openPassword = passwordToOpen.Length > 0 ? passwordToOpen : Type.Missing;
                object writePassword = passwordToModify.Length > 0 ? passwordToModify : Type.Missing;
                object visible = !autoClose;
    
                try
                {
                    double version = double.Parse(app.Version);
    
                    if (version >= 10)
                    {
                        // supports Office XP (2002) and later
                        doc = app.Documents.Open(ref sourceDoc,
                            ref missing, ref missing, ref missing,
                            ref openPassword, ref missing, ref missing,
                            ref writePassword, ref missing,
                            ref missing, ref missing, ref visible, ref missing,
                            ref missing, ref missing, ref missing);
                    }
    
                    if (doc != null)
                    {
                        // activate the document
                        doc.Activate();
    
                        Microsoft.Office.Interop.Word.Range range = app.ActiveDocument.Content;
                        Microsoft.Office.Interop.Word.StoryRanges storyRanges = app.ActiveDocument.StoryRanges;
    
                        if (replacements != null && replacements.Count > 0)
                        {
                            foreach (KeyValuePair<string, string> kvp in replacements)
                            {
                                object findText = kvp.Key;
                                object replaceText = kvp.Value;
    
                                Console.WriteLine("Looking for " + findText + " in main body text");
    
                                if (range.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref missing,
                                    ref missing, ref missing, ref missing, ref findWrap, ref missing,
                                    ref replaceText, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
                                {
                                    Console.WriteLine("Found " + findText + ", replaced with " + replaceText);
                                    continue;
                                }
    
                                foreach (Microsoft.Office.Interop.Word.Range r in storyRanges)
                                {
                                    if (r.NextStoryRange != null)
                                    {
                                        Console.WriteLine("Next range is " + r.Text);
                                        Console.WriteLine("Looking for " + findText + " in text boxes");
    
                                        if (r.NextStoryRange.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref missing,
                                            ref missing, ref missing, ref missing, ref findWrap, ref missing,
                                            ref replaceText, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
                                            Console.WriteLine("Found " + findText + ", replaced with " + replaceText);
                                    }
                                }
                            }
                        }

    Wednesday, January 27, 2010 11:36 AM
  • Hi !

    You unmarked my first post as answer, but it's the answer you need ;)
    The only thing is that you may have missed something when doing your code.

    I think the problem is here, at the end of your code :

            WHILE (r.NextStoryRange != null)
            {
                r = r.NextStoryRange;
                Console.WriteLine("Next range is " + r.Text);
                Console.WriteLine("Looking for " + findText + " in text boxes");

                if (r .Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref missing,
                    ref missing, ref missing, ref missing, ref findWrap, ref missing,
                    ref replaceText, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
                    Console.WriteLine("Found " + findText + ", replaced with " + replaceText);
            }



    Inside the foreach, do not do an if, but a while.
    Then affect to r the r.NextStoryRange.
    Finally, do the Find.Execute with r and not r.NextStoryPage.

    Hope this gets the job done,

    Regards,

    Tom



    Thomas Aimonetti - C# - Sharplog Engineering - http://www.sharplog.fr
    Wednesday, January 27, 2010 1:48 PM
  • Hi kevinjp !

    Did you do the changes I suggested to you and did it work ?

    Regards,

    Tom
    Thomas Aimonetti - C# - Sharplog Engineering - http://www.sharplog.fr
    Monday, February 01, 2010 12:56 AM
  • I could not get the find and replace to work correctly still when the text was to replace was inside text boxes so I have removed the text boxes so that the text to find is in the main body of the word document and now my code works. It was not mandatory that the text boxes needed to be there I found out later.

    Thanks for your assistance. 
    • Marked as answer by Harry Zhu Tuesday, February 09, 2010 8:33 AM
    Monday, February 01, 2010 11:58 AM