Solved

C# Perform a Find/Replace in a word document

Posted on 2010-08-20
5
490 Views
Last Modified: 2012-05-10
Hi,

I have to find/replace text from a word document. It works fine for plain text spread through the document, however when the text is in a textbox, the standard find/replace approach doesn't reach it.

Here is the code for the find/replace that only works with plain text (not in word textbox objects):

 private void FindAndReplace(Word.Application WordApp,
                                    object findText,
                                    object replaceWithText)
        {
            object matchCase = false;
            object matchWholeWord = false;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object nmatchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;

            WordApp.Selection.Find.Execute(ref findText,
                ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundsLike,
                ref nmatchAllWordForms, ref forward,
                ref wrap, ref format, ref replaceWithText,
                ref replace, ref matchKashida,
                ref matchDiacritics, ref matchAlefHamza,
                ref matchControl);
        }
0
Comment
Question by:koldstar
  • 3
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 33494965
here's your solution:
http://www.tech-archive.net/Archive/Office/microsoft.public.office.developer.automation/2007-02/msg00055.html

u need to get the textbox object, then use "text" property to read the text in the textbox control.
0
 

Author Comment

by:koldstar
ID: 33499129
Thanks,

 But InlineShapes contains no objects?
0
 

Author Comment

by:koldstar
ID: 33518759
No,

it just skips the for loop. If I run the Count method it also returns 0.

After some testing I think that the textboxes might be inside an image object. So I guess I have to loop through them first? But how do I do that.
Right now I'm working on another solution where I save the documents as XML first and then do a REGEX "seach and replace". It Seeems to work fine, but It would be nice to know how to do the other way around.



0
 
LVL 9

Assisted Solution

by:Orcbighter
Orcbighter earned 250 total points
ID: 33593950
The trouble with Word is that it is a multi-layered container. Text sits at a higher layer than objects like TextBoxes.
Another example is captions for graphics: The caption and the graphic sit at different layers within the document, which is why they can become separated if the picture is anchored and you add text, or move the picture.
A text search and replace solution would have to be quote complex to ensure you were accessing text at whatever layer it appeared at.
I think your XML solution, which you say works, is a good solution.

0
 

Author Closing Comment

by:koldstar
ID: 33769132
Found another solution
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now