krobelusmeetsyndra
krobelusmeetsyndra

Reputation: 181

Apply bold formatting to specific text on Powerpoint textbox programmatically

I have a code that iterates through all the shapes in a Powerpoint presentation (single slide), finds the one that is a textbox and checks whether it is the one I want to replace the text with (and does so if it is, obviously).

All that is working fine, but I want to set the text bold in 2 parts of the text: the name of the person and the name of the course (it's a diploma). I have tried adjusting the ideas/code from this answer, but to no success.

Could anybody help me? Below is the code I have:

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 [...]";
        }
    }
}

Upvotes: 0

Views: 1125

Answers (1)

Kieran Devlin
Kieran Devlin

Reputation: 1433

TextRange has methods to select a range of text within the TextFrame. For example, .Words(int) will select a selection of words (a set of characters separated via spaces) which you can then apply styles to (in this case .Bold.

Code example:

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

Upvotes: 1

Related Questions