Search code examples
c#ms-wordword-interop

Select Text between two Strings C# Word Interop


I want to select and copy a Range between two given Strings. It's also important to copy a picture or a table. It's hard to explane for me, I hope the example will help:

        Application word = new Application();
        word.Visible = true;
        object findtext = "Favour";
        object findtext2 = "valley";
        Document d2 = word.Documents.Open(@"Path");
        Range range = d2.Content;

        range.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);
        range.Select();
        .........

Text:
Arrived totally in as between private. Favour of so as on pretty though elinor direct. Reasonable estimating be alteration we themselves entreaties me of reasonably. Direct wished so be expect polite valley. Whose asked stand it sense no spoil to. Prudent you too his conduct feeling limited and. Side he lose paid as hope so face upon be. Goodness did suitable learning put.

What I want:
Arrived totally in as between private. Favour of so as on pretty though elinor direct. Reasonable estimating be alteration we themselves entreaties me of reasonably. Direct wished so be expect polite valley. Whose asked stand it sense no spoil to. Prudent you too his conduct feeling limited and. Side he lose paid as hope so face upon be. Goodness did suitable learning put.

Maybe I'm stupid but I work on that like a Week. All my Methods dont copy all the Tabeles and Pictures etc.


Solution

  • The key to something like this is to use two Range objects: one for the starting point and another for the end-point, so that one of the Ranges can then be extended to include the other. I've extended the code from the question to illustrate.

    Note that it's often not necessary to copy content between (or within) Word documents. The Range.FormattedText property makes it possible to quickly transfer formatted information without putting anything on the Clipboard. Both variations are included below.

    Application word = new Application();
    word.Visible = true;
    object findtext = "Favour";
    object findtext2 = "valley";
    Document d2 = word.Documents.Open(@"Path");
    Range range = d2.Content;
    Range rngEnd = null;
    
    range.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);
    rngEnd = range.Duplicate;
    
    rngEnd.Find.Execute(ref findtext2, 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);
    range.End = rngEnd.End;
    
    targetDocument.Paragraphs.Last.Range.FormattedTExt = range.FormattedText;
    //range.Copy();