如何存储文本';运行VBA之前的原始格式?

如何存储文本';运行VBA之前的原始格式?,vba,powerpoint,Vba,Powerpoint,我有一些代码,可以通过预先确定的颜色(黑色>红色>绿色>黑色等)切换选定文本的字体颜色。不过,我真正想做的是,使我的演示文稿无论采用何种格式,都更具动态性 例如,如果我在幻灯片标题的一部分(格式为蓝色)上运行宏,我就没有办法回到原来的蓝色。我曾考虑过将格式“复制”到某种变量中,但我不确定如何准确地清除该变量或将其存储以备将来使用 更可能的是构建某种“上下文”搜索。如果我可以查看所选文本的左侧和右侧,可能会有一些输入输入到文本应该是什么颜色(因为它很少需要在文本块中有所不同)。但这很快超出了我的专

我有一些代码,可以通过预先确定的颜色(黑色>红色>绿色>黑色等)切换选定文本的字体颜色。不过,我真正想做的是,使我的演示文稿无论采用何种格式,都更具动态性

例如,如果我在幻灯片标题的一部分(格式为蓝色)上运行宏,我就没有办法回到原来的蓝色。我曾考虑过将格式“复制”到某种变量中,但我不确定如何准确地清除该变量或将其存储以备将来使用

更可能的是构建某种“上下文”搜索。如果我可以查看所选文本的左侧和右侧,可能会有一些输入输入到文本应该是什么颜色(因为它很少需要在文本块中有所不同)。但这很快超出了我的专业知识

有关当前设置,请参阅下面的代码

Sub TextColorSwap()

On Error Resume Next

With ActiveWindow.Selection
    If .TextRange.Font.Color = RGB(0, 0, 0) Then
        .TextRange.Font.Color = vbRed
    ElseIf .TextRange.Font.Color = vbRed Then
        .TextRange.Font.Color = RGB(0, 153, 0)
    Else
        .TextRange.Font.Color = RGB(0, 0, 0)
    End If
End With

End Sub
专业人士有什么想法吗


-R

像这样的东西让你开始吧:

Option Explicit

Public bFirstColour As Boolean

Sub TextColorSwap()

  ' This will mask all errors, and if you don't have your own error handler,
  ' is bad practice as you'll never know if something's gone wrong.
  On Error Resume Next

  Dim lColour As Long
  Dim lCursor As Long

  With ActiveWindow.Selection

    If .Type = ppSelectionText Then
      ' Get the start position of the selected text
      lCursor = .TextRange.Start
      ' If there are characters to the left of the selected text, save the colour and set a flag
      If lCursor > 1 And Not bFirstColour Then
        lColour = .TextRange.Characters(1).Font.Color
        bFirstColour = True
      End If
      ' Toggle the colour
      With .TextRange.Font
        If .Color = lColour Then
          .Color = vbRed
        ElseIf .Color = vbRed Then
          .Color = RGB(0, 153, 0)
        Else
          ' Reset the colour to the colour fond to the left of the text
          .Color = lColour
          bFirstColour = False
        End If
      End With
    End If
  End With
End Sub

像这样的东西让你开始吧:

Option Explicit

Public bFirstColour As Boolean

Sub TextColorSwap()

  ' This will mask all errors, and if you don't have your own error handler,
  ' is bad practice as you'll never know if something's gone wrong.
  On Error Resume Next

  Dim lColour As Long
  Dim lCursor As Long

  With ActiveWindow.Selection

    If .Type = ppSelectionText Then
      ' Get the start position of the selected text
      lCursor = .TextRange.Start
      ' If there are characters to the left of the selected text, save the colour and set a flag
      If lCursor > 1 And Not bFirstColour Then
        lColour = .TextRange.Characters(1).Font.Color
        bFirstColour = True
      End If
      ' Toggle the colour
      With .TextRange.Font
        If .Color = lColour Then
          .Color = vbRed
        ElseIf .Color = vbRed Then
          .Color = RGB(0, 153, 0)
        Else
          ' Reset the colour to the colour fond to the left of the text
          .Color = lColour
          bFirstColour = False
        End If
      End With
    End If
  End With
End Sub