1

我编写了代码来查找 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;

         }

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

4

1 回答 1

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;
}
于 2020-02-14T06:47:10.810 回答