向幻灯片添加形状并对其进行格式化

时间:2014-07-01 10:01:59

标签: vba powerpoint-vba

我正在尝试在下面创建我的vba脚本,为powerpoint幻灯片添加注释注释。我们的想法是,该脚本可用于添加待检查的笔记"滑动。因此,我已将其设置在一个显示菜单的小插件中,因此添加了添加TBC,TBU,TBD注释。 该子不时显示错误,并不总是完全发挥作用(我想因为我在我的代码中写的部分:

ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select

任何人都可以帮助我如何使脚本防弹。对该方法的简短解释将是很好的。这样我就可以了解未来的事情。

最佳,

eltiburon

这是我到目前为止的整个脚本:

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

1 个答案:

答案 0 :(得分:4)

这看起来像早期版本的PPT中由宏录制器生成的代码。

首先,永远不要在代码中选择任何内容,除非它是绝对必要的(并且它很少)。请改用形状参考(正如您在回复其他问题时发布的其他几个示例中所见)。

因为录制的宏假定您正在使用名为Rectangle 4的形状,所以只有在已经有三个矩形的幻灯片上运行它时,它才会起作用。所以相反:

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