Solved

C# Perform a Find/Replace in a word document

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

635 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