向幻灯片添加形状并设置格式 [英] Add a shape to a slide and format that
问题描述
我正在尝试在下面制作我的 vba 脚本,以便为 powerpoint 幻灯片添加注释.这个想法是该脚本可用于向幻灯片添加待检查笔记".因此,我在一个显示菜单的小插件中设置了它,因此添加了 TBC、TBU、TBD 注释.sub 不时显示错误并且并不总是完全完成它的工作(我猜是因为我在代码中写的部分:
I am trying to make my vba script below to add annotation notes to powerpoint slides. The idea is that the script can be used to add "to-be-checked notes" to slides. Hence, I've got it set up in a little add-in that displays a menu so adding the TBC, TBU, TBD notes are added. The sub is showing errors from time to time and does not always fully do its job (i guess because of the part where I wrote in my code:
ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select
任何人都可以帮助我如何使脚本防弹.对该方法的简短解释会很棒.这样我就可以学习如何在未来做正确的事情.
Can anyone assist me how do make the script bulletproof. A short explanation of the approach would be great. That way I can learn how do things right in the future.
最好,
这是我目前的整个脚本的样子:
This is what my entire script so far looks like:
Sub InsertShape_TBC()
ActiveWindow.Selection.SlideRange.Shapes.AddShape(msoShapeRectangle, 575.5, 9.12, 124.75, 34.12).Select
With ActiveWindow.Selection.ShapeRange
.Fill.Visible = msoTrue
.Fill.Solid
.Fill.ForeColor.RGB = RGB(162, 30, 36)
.Fill.Transparency = 0#
.Line.Visible = msoFalse
End With
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(Start:=1, Length:=0).Select
With ActiveWindow.Selection.TextRange
.Text = "[TBC]"
With .Font
.Name = "Arial"
.Size = 18
.Bold = msoFalse
.Italic = msoFalse
.Underline = msoFalse
.Shadow = msoFalse
.Emboss = msoFalse
.BaselineOffset = 0
.AutoRotateNumbers = msoFalse
.Color.SchemeColor = ppForeground
End With
End With
ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(Start:=1, Length:=6).Select
ActiveWindow.Selection.TextRange.Font.Bold = msoTrue
ActiveWindow.Selection.TextRange.Font.Color.RGB = RGB(Red:=255, Green:=255, Blue:=255)
ActivePresentation.ExtraColors.Add RGB(Red:=255, Green:=255, Blue:=255)
ActiveWindow.Selection.Unselect
End Sub
推荐答案
这看起来像是早期版本的 PPT 中宏记录器生成的那种代码.
This looks like the kind of code produced by the macro recorder in earlier versions of PPT.
首先,永远不要在代码中选择任何东西,除非绝对有必要这样做(而且很少这样做).改用形状引用(正如您在我为回答您的其他问题而发布的其他几个示例中所见).
First off, never select anything in code unless it's absolutely necessary to do so (and it seldom is). Use shape references instead (as you've seen in a couple of the other examples I posted in response to your other questions).
因为录制的宏假定您正在使用名为 Rectangle 4 的形状,所以只有在已经具有三个矩形的幻灯片上运行它时,它才会起作用.所以改为:
Because the recorded macro assumes that you're working with a shape called Rectangle 4, it will only work if you run it on a slide that has three rectangles already. So instead:
Dim oSh as Shape
Set oSh = ActiveWindow.Selection.SlideRange.Shapes.AddShape(msoShapeRectangle, 575.5, 9.12, 124.75, 34.12)
' Notice that I removed the .Select from the end of your code.
' We don't want to select anything if we don't have to.
' Then
With oSh
With .TextFrame.TextRange
.Text = "[TBC]"
With .Font
.Name = "Arial"
.Size = 18
.Bold = msoFalse
.Italic = msoFalse
.Underline = msoFalse
.Shadow = msoFalse
.Emboss = msoFalse
.BaselineOffset = 0
.AutoRotateNumbers = msoFalse
.Color.SchemeColor = ppForeground
End With ' Font
End with ' TextRange
End With ' oSh, the shape itself
这篇关于向幻灯片添加形状并设置格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!