gpt4 book ai didi

vba - 将形状添加到幻灯片并设置格式

转载 作者:行者123 更新时间:2023-12-02 21:25:43 25 4
gpt4 key购买 nike

我正在尝试制作下面的 vba 脚本来向 powerpoint 幻灯片添加注释。这个想法是该脚本可用于向幻灯片添加“待检查的注释”。因此,我将其设置在一个显示菜单的小插件中,以便添加 TBC、TBU、TBD 注释。该子系统时不时地显示错误,并且并不总是完全完成其工作(我猜是因为我在代码中编写的部分:

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

任何人都可以帮助我如何使脚本防弹。对这种方法的简短解释会很好。这样我就可以学习将来如何做正确的事情。

最好,

艾替布隆

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

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 中宏记录器生成的代码。

首先,永远不要在代码中选择任何内容,除非绝对有必要这样做(而且很少这样做)。请改用形状引用(正如您在我为回答您的其他问题而发布的其他几个示例中所看到的那样)。

由于录制的宏假定您正在使用名为“矩形 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

关于vba - 将形状添加到幻灯片并设置格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24507469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com