VB.Net VSTO PowerPoint 插件
VB.Net VSTO PowerPoint Addin
我正在制作 PowerPoint 2013 的加载项。我的目标是将我在幻灯片上找到的所有方程式转换为普通文本,以更改这些方程式的字体。
因为它不会让我在它们是方程式时更改字体。我通过遍历文本范围并查找字体名称设法找到了方程式,它们使用 "Cambria Math"。所以我的问题是如何以编程方式将方程式更改为普通文本,就像方程式工具中的按钮一样?似乎出于某种原因,他们从 PowerPoint 中删除了 "record macro",因此我无法从中获得帮助。
我尝试用 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
我在 VBA 中将它与 PowerPoint 2016 一起使用。我的字体列表中没有“Calibri”,所以我将其更改为“Calibri(正文)”并且它有效。这可能与您在使用 .NET VSTO 插件时遇到的问题相同。如果我有时间,我将构建一个 VSTO 插件的示例并 post 结果。
视频
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
我正在制作 PowerPoint 2013 的加载项。我的目标是将我在幻灯片上找到的所有方程式转换为普通文本,以更改这些方程式的字体。
因为它不会让我在它们是方程式时更改字体。我通过遍历文本范围并查找字体名称设法找到了方程式,它们使用 "Cambria Math"。所以我的问题是如何以编程方式将方程式更改为普通文本,就像方程式工具中的按钮一样?似乎出于某种原因,他们从 PowerPoint 中删除了 "record macro",因此我无法从中获得帮助。
我尝试用 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
我在 VBA 中将它与 PowerPoint 2016 一起使用。我的字体列表中没有“Calibri”,所以我将其更改为“Calibri(正文)”并且它有效。这可能与您在使用 .NET VSTO 插件时遇到的问题相同。如果我有时间,我将构建一个 VSTO 插件的示例并 post 结果。
视频
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