Solved

C# Perform a Find/Replace in a word document

Posted on 2010-08-20
5
494 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

22 Experts available now in Live!

Get 1:1 Help Now