views:

83

answers:

2

How I can change this feature so I select the range of characters in a word document between the characters "E" and "F", if I have; xasdasdEcdscasdcFvfvsdfv is underlined to me the range -> cdscasdc

private void Rango()
{
Word.Range rng;

Word.Document document = this.Application.ActiveDocument;

object startLocation = "E";
object endLocation = "F";

// Supply a Start and End value for the Range. 
rng = document.Range(ref startLocation, ref endLocation);

// Select the Range.
rng.Select();

}

This function will not let me pass by reference two objects of string type.......

Thanks

A: 

You need to pass the position in the document you want the range to cover, see: How to: Define and Select Ranges in Documents

I have added some example code below:

        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        string document = null;
        using (OpenFileDialog dia = new OpenFileDialog())
        {
            dia.Filter = "MS Word (*.docx)|*.docx";
            if (dia.ShowDialog() == DialogResult.OK)
            {
                document = dia.FileName;
            }
        }           

        if (document != null)
        {
            Document doc = word.Documents.Open(document, ReadOnly: false, Visible: true);
            doc.Activate();
            string text = doc.Content.Text;

            int start = text.IndexOf('E') + 1;
            int end = text.IndexOf('F');
            if (start >= 0 && end >= 0 && end > start)
            {
                Range range = doc.Range(Start: start, End: end);
                range.Select();
            }
        }

Do not forget to close the document and Word etc.

Mikael
A: 

Firstly thank you for your reply ...:) I compiled the code and I get two errors in the lines you have mentioned. I missing some parameter or do not I put where it should be? This is the complete code;

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Word = Microsoft.Office.Interop.Word; using Office = Microsoft.Office.Core; using Microsoft.Office.Tools.Word; using Microsoft.Office.Tools.Word.Extensions;

namespace WordAddIn1 { public partial class ThisAddIn {

    private void Rango()
    {
        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        string document = null;
        using (OpenFileDialog dia = new OpenFileDialog())
        {
            dia.Filter = "MS Word (*.docx)|*.docx";
            if (dia.ShowDialog() == DialogResult.OK)
            {
                document = dia.FileName;
            }
        }           

        if (document != null)
        {
            ***Document doc = word.Documents.Open(document, ReadOnly: false, Visible: true);***
            doc.Activate();
            string text = doc.Content.Text;

            int start = text.IndexOf('E') + 1;
            int end = text.IndexOf('F');
            if (start >= 0 && end >= 0 && end > start)
            {
                ***Range range = doc.Range(Start: start, End: end);***
                range.Select();
            }
        }

   }
   private void ThisAddIn_Startup(object sender, System.EventArgs e)
   {
       Rango();
   }
   private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code

}

Petott