[英]Powerpoint - VBA - Put selected text into square bracket
我想创建一个在powerpoint上可用的宏,以将所选文本放在方括号中。 我找到了可以在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
例如:如果我在Powerpoint中选择“ thisisatext”并激活了宏,结果将为“ [thisisatext]”; 如果我仍在选择“ [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
结束子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.