2017-03-27 1 views
0

est-il possible de trouver un nouveau match dans une plage spécifiée s'il y a 2 correspondances dans la plage. ou s'il y a un moyen d'obtenir une liste de tous les matchs. Actuellement, j'utilise la méthode interop ci-dessous pour trouver la correspondance, mais elle ne mettra en évidence que le premier match.Rechercher une correspondance dans une plage dans le document Word

bool found = foundRange.Find.Execute(ref oFindText, 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); 

Merci.

Répondre

1

Voir commentaires en ligne:

//Get any range you want 
var range = app.ActiveDocument.StoryRanges[WdStoryType.wdMainTextStory]; 


var document = range.Document; 

//We want the variable range to continue refering to the same Range at all times 
var foundRange = range.Duplicate; 

if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)) { 
    Console.WriteLine("Found first"); 
    //After using .Execute(), FoundRange has been set to the found text 
    Console.WriteLine(foundRange.Text); 
} 
else 
{ 
    Console.WriteLine("Didn't find first"); 
} 

//Set foundRange to start at the character after the last find and to end where the original range ends 
foundRange = document.Range(foundRange.Start + 1, range.End); 

//Repeat. Obviously you could use some kind of loop 
if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)) 
{ 
    Console.WriteLine("Found second"); 
    Console.WriteLine(foundRange.Text); 
} 
else 
{ 
    Console.WriteLine("Didn't find second"); 
} 

foundRange.Select(); //Just to verify. We don't need the selection object for anything