【问题标题】:VBA Powerpoint - Combining normal text and equation inside a textboxVBA Powerpoint - 在文本框中结合普通文本和方程式
【发布时间】:2026-01-30 08:40:01
【问题描述】:

尝试将一些正常书写的文本和方程式结合起来(使用 powerpoint 中内置的插入方程式功能)。手动手动完成很容易(输入普通文本,然后单击插入新方程或按 alt 和 +),但 VBA 对我来说很棘手。

我已经设法在单独的文本框中执行此操作,但是当我尝试在一个文本框中一起执行时,它会将文本框中的所有文本格式化为一个等式。

例如'Solve 4x+2=8' 我希望将 'solve' 写为普通字符串/文本,但是,我希望使用 powerpoint 插入方程功能来编写 4x+2=8 部分。

我在这里搜索过,发现这段代码可以在一个独立的框中编写一个方程式。但是当我添加世界求解时,它会将其视为方程式而不是普通文本。我什至尝试将“解决”连接为字符串。

Sub insert_equation()

Dim a As Integer
a = 2

Dim word As String

word = "Solve "

  Application.CommandBars.ExecuteMso ("InsertBuildingBlocksEquationsGallery")

  With ActiveWindow.Selection.ShapeRange.TextFrame
  With .TextRange
      .Font.Size = 22
      .Text = word & "2x^2+7x+6=0"
      '.Text = a & "x"
  End With
  End With

  Application.CommandBars.ExecuteMso ("EquationProfessional")

End Sub

在下图中,第二个文本框是代码返回的,第三个文本框是我希望实现的。我希望能够在文本框中的不同位置混合使用方程式和普通文本。

example

非常感谢。

【问题讨论】:

    标签: excel vba office365 powerpoint equation


    【解决方案1】:
    Sub WhatIsTheEquationMadeOf()
        Dim x As Long, txtBox As Shape
        word = "Solve "
        
        Set txtBox = ActiveWindow.Selection.ShapeRange(1)
        If txtBox.Type = msoTextBox Then
            With txtBox.TextFrame.TextRange
                .Font.Color = RGB(80, 122, 160)
                .ParagraphFormat.Alignment = ppAlignCenter
                .Font.Size = 22
                .Text = word & "2x^2+7x+6=0"
                .Characters(Len(word) + 1, .Characters.Count - Len(word)).Select
                With Application.CommandBars
                    .ExecuteMso "InsertBuildingBlocksEquationsGallery"
                    .ExecuteMso "EquationProfessional"
                End With
            End With
        End If
    End Sub
    

    之前

    之后

    【讨论】:

      【解决方案2】:

      我知道的一种方法是使用 .Characters 对象。

      Sub insert_equation()
      
      Dim a As Integer
      a = 2
      
      Dim word As String
      
      word = "Solve "
      
        Application.CommandBars.ExecuteMso ("InsertBuildingBlocksEquationsGallery")
      
        With ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters()
              .Font.Size = 22
              .Text = word
              .Font.Italic = False
              .Font.Color = vbRed
          End With
      
          With ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(Len(word) + 1)
                .Font.Size = 32
                .Text = "2x^2+7x+6=0"
                .Font.Italic = True
                .Font.Color = vbBlue
          End With
      
        Application.CommandBars.ExecuteMso ("EquationProfessional")
      
      End Sub
      

      你也可以在这里阅读Characters object (Excel)

      【讨论】: