Powerpoint - VBA - 将所选文本放入方括号

Powerpoint - VBA - Put selected text into square bracket

我想创建一个可以在 powerpoint 上运行的宏,将 selected 文本放入方括号中。我找到了适用于 Excel 的代码,但我需要针对 PowperPoint 对其进行调整。但是,经过几次尝试,我不知道如何纠正它。 你能帮我吗?

Sub other_Brackets() ' Keyboard shortcut: Ctrl + Shift + T
Dim Form As String
Set def = Selection
For Each bracket In Selection
    bracket.Select
    Form = ActiveCell.Formula
    If Left(Form, 1) = "[" Or Right(Form, 1) = "]" Then
        If Left(Form, 1) = "[" Then
            Form = Right(Form, Len(Form) - 1)
        End If
        If Right(Form, 1) = "]" Then
            Form = Left(Form, Len(Form) - 1)
        End If
        ActiveCell.Formula = Form
    Else
        ActiveCell.Formula = "[" & Form & "]"
    End If
Next
Application.GoTo def
End Sub

示例:如果我 select 在 Powerpoint "thisisatext" 中激活宏,结果将是“[thisisatext]”;如果我仍在 selection“[thisisatext]”上并激活宏,结果将是 "thisisatext".

主要问题(不止一个)是 excel 代码在活动单元格上运行。在 powerpoint 上,你可以只使用 ActiveWindow.Selection

这应该有效:

Sub other_Brackets() ' Keyboard shortcut: Ctrl + Shift + T
Dim selectedText As String
Dim replacedText As String
selectedText = ActiveWindow.Selection.TextRange
If Left(selectedText, 1) = "[" Or Right(selectedText, 1) = "]" Then
    replacedText = Right(selectedText, Len(selectedText) - 1)
    replacedText = Left(replacedText, Len(replacedText) - 1)
    ActiveWindow.Selection.TextRange = replacedText
Else
    ActiveWindow.Selection.TextRange = "[" & selectedText & "]"
End If

结束子