Solved

C# Perform a Find/Replace in a word document

Posted on 2010-08-20
5
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

730 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