Word 互操作将样式应用于所有匹配项

Word interop apply style to all the matches

我已经编写了代码来查找 word 文件中的引文,并试图使所有引文都上标。

Word.Range rngCitations = doc.Content;
        rngCitations.Find.MatchWildcards = true;
        rngCitations.Find.Text = @"(\[[0-9]{1,}[,0-9]*\])";
        if (rngCitations.Find.Execute())
        {
            rngCitations.Font.Superscript = 1;
            rngCitations.Font.Bold = 1;

         }

但是,只有第一个匹配更新为上标。如何更改此代码以将样式应用于所有匹配项。

循环可以使用官方的例子: https://docs.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-loop-through-found-items-in-documents?view=vs-2019

rng.Find.ClearFormatting(); 
rng.Find.Forward = true; 
rng.Find.Text = "find me"; 

rng.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 (rng.Find.Found) 
{ 
    intFound++;
    rng.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);
}

在你的情况下改变 if with while

while (rngCitations.Find.Execute())
{
      rngCitations.Font.Superscript = 1;
      rngCitations.Font.Bold = 1;
}