user2308890
user2308890

Reputation:

Find a word location with wordApp.ActiveDocument.Range

I'm working a code that finds a placeholder in Word document ex. XXTestXX. I'm using this code to replace text in the document. What I can't get to work is finding the start and end of a placeholder.

What is the best way to find the range of the playholder?

        object RangeStart = 5;
        object RangeEnd = 10;
        Word.Range rng = wordApp.ActiveDocument.Range(ref RangeStart, ref RangeEnd);
        rng.Text = replaceText.ToString();
        rng.Select();

I've used wordApp.Selection.Find.Execute to replace text but it has a limitation of 250 characters.

Thanks

Upvotes: 0

Views: 817

Answers (1)

user2308890
user2308890

Reputation:

Fix it by using this code.

        Word.Range rng = aDoc.Content;
        rng.Find.ClearFormatting();
        rng.Find.Forward = true;
        rng.Find.Text = findText.ToString();

        rng.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

        while (rng.Find.Found)
        {
            object RangeStart = rng.Start;
            object RangeEnd = rng.End;
            Word.Range rng1 = wordApp.ActiveDocument.Range(ref RangeStart, ref RangeEnd);
            rng1.Text = replaceText.ToString();
            rng1.Select();

            rng.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        }

Upvotes: 1

Related Questions