vbavstopowerpointpowerpoint-2013

VB.Net VSTO PowerPoint Addin


I'm making an Add-in for PowerPoint 2013. My goal is to convert all equations that I find on slides to normal text, to change the font of those equations. Because it won't let me change font while they are equations. I managed to find the equations, by iterating through text ranges and finding font name, they use "Cambria Math". So my question is how can programmatically change equations to normal text, Like the button in equation tools does? And it seems for some reason they removed "record macro" from PowerPoint, so I couldn't get help from that. I tried recording macro in word and doing the same thing, and i got: Selection.OMaths(1).ConvertToMathText, but it doesn't seem to be OMaths in PowerPoint.

Dim Application As PowerPoint.Application = New PowerPoint.Application
        Dim Presentation As PowerPoint.Presentation = Application.ActivePresentation
        Dim Windows As PowerPoint.DocumentWindows = Application.Windows

        For Each Slide As PowerPoint.Slide In Presentation.Slides
            For Each Shape As PowerPoint.Shape In Slide.Shapes
                For Each Paragraph As PowerPoint.TextRange In Shape.TextFrame.TextRange
                    For Each Line As PowerPoint.TextRange In Paragraph.Lines
                        If Line.Font.Name = "Cambria Math" Then
                            With Line.Font
                                .Name = "Calibri"
                                .Bold = True
                            End With
                        ElseIf Line.Font.Name = "Calibri" Then
                            With Line.Font
                                .Name = "Palatino"
                            End With
                        End If
                    Next Line
                Next Paragraph
            Next Shape
            Next Slide
    End Sub

Other text here is changed normally, but equations the ones with "Math Cambria" font, are unchanged.

I also tried to get selection, then something with OMaths, like in Word Vsto, but, it seems OMaths is not part of the PowerPoint. This next code is actually supposed to change it to equation, but i guess if it worked, could have find a way to reverse it.

For Each Window As PowerPoint.DocumentWindow In Windows
    Selection.OMaths(1).ConvertToMathText
Next Window

Solution

  • I got it to work with PowerPoint 2016 in VBA. I didn't have "Calibri" in my list of fonts, so I changed it to "Calibri (Body)" and it works. It may be the same issue you're having with the .NET VSTO Addin. If I have time, I'll build a example of the VSTO Addin and post the results as well.

    Video

    video

    VBA Code

    Public Sub UpdateShapeFont()
    On Error GoTo ErrTrap
    Dim Application     As PowerPoint.Application: Set Application = New PowerPoint.Application
    Dim Presentation    As PowerPoint.Presentation: Set Presentation = Application.ActivePresentation
    Dim Windows         As PowerPoint.DocumentWindows: Set Windows = Application.Windows
    Dim Slide           As PowerPoint.Slide
    Dim Shape           As PowerPoint.Shape
    Dim Paragraph       As PowerPoint.TextRange
    Dim line            As PowerPoint.TextRange
    
        For Each Slide In Presentation.Slides
            For Each Shape In Slide.Shapes
                For Each Paragraph In Shape.TextFrame.TextRange
                    For Each line In Paragraph.Lines
                        Select Case line.Font.Name
                            Case "Cambria Math"
                                With line.Font
                                    .Name = "Calibri (Body)" 'check if the font exists in your list of fonts; it did not work for "Calibri"
                                    .Bold = True
                                End With
                            Case "Calibri"
                                With line.Font
                                    .Name = "Palatino"
                                End With
                        End Select
                    Next line
                Next Paragraph
            Next Shape
        Next Slide
                
    ExitProcedure:
        On Error Resume Next
        Exit Sub
    
    ErrTrap:
        Select Case Err.number
            Case Else
                Debug.Print "Error #: " & Err.number & " |Error Description: " & Err.description
        End Select
        Resume ExitProcedure
        Resume 'for debugging
        
    End Sub