首页 > 解决方案 > VB.Net VSTO PowerPoint 插件

问题描述

我正在为 PowerPoint 2013 制作插件。我的目标是将我在幻灯片上找到的所有方程式转换为普通文本,以更改这些方程式的字体。因为当它们是方程式时,它不会让我改变字体。我设法通过遍历文本范围并找到字体名称来找到方程式,他们使用“Cambria Math”。所以我的问题是如何以编程方式将方程更改为普通文本,就像方程工具中的按钮一样?似乎出于某种原因,他们从 PowerPoint 中删除了“录制宏”,所以我无法从中获得帮助。我尝试用 word 录制宏并做同样的事情,我得到了:Selection.OMaths(1).ConvertToMathText,但它似乎不是 PowerPoint 中的 OMaths。

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

此处的其他文字正常更改,但带有“Math Cambria”字体的方程式不变。

我还尝试进行选择,然后使用 OMaths,例如在 Word Vsto 中,但是,似乎 OMaths 不是 PowerPoint 的一部分。下一个代码实际上应该将其更改为方程式,但我想如果它有效,可能会找到一种方法来扭转它。

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

标签: vbavstopowerpointpowerpoint-2013

解决方案


我让它在 VBA 中与 PowerPoint 2016 一起工作。我的字体列表中没有“Calibri”,所以我将其更改为“Calibri (Body)”并且它可以工作。这可能与您在使用 .NET VSTO 插件时遇到的问题相同。如果我有时间,我将构建 VSTO 插件的示例并发布结果。

视频

视频

VBA 代码

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

推荐阅读