在 VSTO C# 函数中查找和替换,没有任何将替换文本设为粗体的规定

Find and replace in VSTO C# function, does not have any provision for making the replaced text bold

为了查找并替换为粗体文本,我正在尝试将我的 VBA 子例程转换为 VSTO C# 函数。

从 VB 到 C# 的简单翻译不会替换任何内容(可能是因为查找文本和替换文本必须作为参数传递给 Execute 调用,而不是从 "oPara.Range.Find" 参数。提供命名参数的修改替换了文本,但似乎没有办法制作替换文本 "Bold"。VB 有什么特别之处?我该如何实现it in C#?设置参数"oPara.Range.Find"好像对C#没用

我已经尝试使用下面的代码。 C# 中的第二个 Execute 调用有效,但我无法制作替换文本 BOLD.

Sub ReplaceWithBoldInVBA()
    Dim oPara As Object
    Set oPara = Selection.Paragraphs(1)
    oPara.Range.Select

    Dim bFound As Boolean
    With oPara.Range.Find
        .ClearFormatting
        .Text = "Test- "
        .Replacement.Text = "Test: "
        .Replacement.ClearFormatting
        .Replacement.Font.Bold = True
        .Replacement.Font.Italic = False
        .Replacement.Font.Underline = False
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        bFound = .Execute(Replace:=wdReplaceOne)
    End With
End Sub

// Following is the VSTO Add-In C# code 
using Word = Microsoft.Office.Interop.Word;
public void ReplaceWithBoldIn_VSTO_AddIn_CSharp() {
    Word.Paragraph oPara = Globals.ThisAddIn.Application.Selection.Paragraphs[1];
    oPara.Range.Select();

    oPara.Range.Find.ClearFormatting();
    oPara.Range.Find.Text = "Test- ";
    oPara.Range.Find.Replacement.Text = "Test: ";
    oPara.Range.Find.Replacement.ClearFormatting();
    oPara.Range.Find.Replacement.Font.Bold = -1;
    oPara.Range.Find.Replacement.Font.Italic = 0;
    oPara.Range.Find.Replacement.Font.Underline = 0;
    oPara.Range.Find.Forward = true;
    oPara.Range.Find.Wrap = Word.WdFindWrap.wdFindStop;
    oPara.Range.Find.Format = true;
    oPara.Range.Find.MatchCase = true;
    oPara.Range.Find.MatchWholeWord = false;
    oPara.Range.Find.MatchWildcards = false;
    oPara.Range.Find.MatchSoundsLike = false;
    oPara.Range.Find.MatchAllWordForms = false;

    oPara.Range.Find.Execute(Replace: Word.WdReplace.wdReplaceOne);

    // The following Execute call works but then I can not make the replaced text **BOLD**.


    oPara.Range.Find.Execute(FindText: "Test- ", ReplaceWith: "Test: ", Replace: Word.WdReplace.wdReplaceOne)   
}

C# 遇到困难的原因是应用 Find.Execute 的确切 Range 不够清楚。 VBA 可以做到这一点是因为 With - C# 代码中缺少该关联。

以下适合我。请注意特定的 RangeFind 对象是如何声明和用于分配 Find 属性的,并且相同的对象用于 Execute。 (注意:现在声明和实例化 rngFind 并不是真正必要的。在 VSTO 之外的 C# 的早期,它是,现在使用它是个人喜好的问题。)

        Word.Paragraph oPara = Globals.ThisAddIn.Application.Selection.Paragraphs[1];
        Word.Range rngFind = oPara.Range;
        Word.Find fnd = rngFind.Find;
        fnd.ClearFormatting();
        fnd.Text = "Test- ";
        fnd.Replacement.Text = "Test: ";
        fnd.Replacement.ClearFormatting();
        fnd.Replacement.Font.Bold = -1;
        fnd.Replacement.Font.Italic = 0;
        fnd.Replacement.Font.Underline = 0;
        fnd.Forward = true;
        fnd.Wrap = Word.WdFindWrap.wdFindStop;
        fnd.Format = true;
        fnd.MatchCase = true;
        fnd.MatchWholeWord = false;
        fnd.MatchWildcards = false;
        fnd.MatchSoundsLike = false;
        fnd.MatchAllWordForms = false;

        fnd.Execute(Replace: Word.WdReplace.wdReplaceOne);