2010-10-25 14 views
10

puedo encontrar un texto en un archivo de Word a través de:C#: Búsqueda de un texto en Word y conseguir el rango del resultado

Word.Range range = wordApp.ActiveDocument.Content; 
Word.Find find = range.Find; 
find.Text = "xxx"; 
find.ClearFormatting(); 
find.Execute(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, ref missing); 

Esto me dice que si se encuentra el texto. Pero necesito el rango de la pieza de texto encontrada.

+0

Anterior en el código: objeto privado missing = Type.Missing; – simon

+0

No estoy seguro de ustedes chicos/chicas, pero esta API me pareció un poco confusa al principio. Esperaría que 'range.Find.Execute (..)' devuelva un nuevo objeto de rango en lugar de cambiar el que está en la raíz ('Document.Content'). –

Respuesta

3

El objeto de rango debe cambiarse ejecutando find en él.

Por lo tanto, es probable que utilice range.Start y range.End para obtener las posiciones de los caracteres. Reference

8

Has probado esto:

range.Find.Execute(
     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, ref missing); 


while (range.Find.Found) 
{ 
    //Get selected index. 
    // Do as you please with range... 
    //Positions: range.Start... range.End 
    //search again 
    range.Find.Execute(
     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, ref missing); 
} 
+1

Usted dice en su código "// Obtener índice seleccionado". ¿Puedes escribir el código aquí? Estás asumiendo que las personas saben cómo obtener el índice seleccionado. –

2

consigue un rango de palabra con el método de búsqueda y darle formato.

//Parameter contains what you want to find. 
_wordApp.Selection.Find.Execute(title); 

Word.Range range = _wordApp.Selection.Range; 
if (range.Text.Contains(title)) 
{ 
    //gets desired range here it gets last character to make superscript in range 
    Word.Range temprange = _document.Range(range.End - 1, range.End);   
    temprange.Select(); 
    Word.Selection currentSelection = _wordApp.Selection; 
    currentSelection.Font.Superscript = 1; 
} 
2

range.Find.Execute vuelve true si lo encuentra, y establece range a la gama encontrado:

var range = doc.Range(); 
while (range.Find.Execute("xxx")) 
    Debug.Print(range.Text); 

Tenga en cuenta que range.Find.Execute buscará el rango después range si range ya está a la altura de la Encuentra condiciones (después de la primer range.Find.Execute).

Por ejemplo, esta macro VBA sólo encontrará la segunda "B":

Sub Macro1() 
    ActiveDocument.Range.Text = "abba" 
    Dim r As Range 
    Set r = ActiveDocument.Range(1, 2) ' the first "b" 
    Debug.Print r.Start; r.End  ' prints " 1 2 " 

    Debug.Print r.Find.Execute("b") ' prints "True" 
    Debug.Print r.Start; r.End  ' prints " 2 3 " 

    Debug.Print r.Find.Execute("b") ' prints "False" (if r.Find.Wrap = wdFindStop) 
    Debug.Print r.Start; r.End  ' prints " 2 3 " 
End Sub 
-2

Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;

Cuestiones relacionadas