以编程方式将粗体格式应用于 Powerpoint 文本框上的特定文本

Apply bold formatting to specific text on Powerpoint textbox programmatically

我有一个代码可以遍历 Powerpoint 演示文稿(单张幻灯片)中的所有形状,找到一个文本框并检查它是否是我想要用来替换文本的那个(如果显然是)。

一切正常,但我想在文本的两个部分中将文本设置为粗体:人名和课程名称(这是一个文凭)。我尝试从 this 的答案中调整 ideas/code,但没有成功。

有人能帮帮我吗? 下面是我的代码:

Presentation certificadoCOM = powerpointApp.Presentations.Open(@"C:\Users\oru1ca\Desktop\certCOM.pptx");
// iterates through all shapes
foreach (Shape shape in certificadoCOM.Application.ActivePresentation.Slides.Range().Shapes)
{
    // gets the name of the shape and checks whether is a textbox
    string shapeName = shape.Name;
    if (shapeName.StartsWith("Text Box"))
    {
        // gets the text from the shape, and if it's the one to change, replace the text
        string shapeText = shape.TextFrame.TextRange.Text;
        if (shapeText.StartsWith("Concedemos"))
        {
             shape.TextFrame.TextRange.Text = "Concedemos à Sra. " + nomeP[i] + ",\n representando [...]";
        }
    }
}

TextRange 具有 select TextFrame 范围内的文本的方法。 例如,.Words(int) 将 select 一个 select 词组(一组由空格分隔的字符),然后您可以对其应用样式(在本例中为 .Bold.

代码示例:

//Set the first 3 words as bold.
shape.TextFrame.TextRange.Words(3).Font.Bold = true;