为什么访问形状对象时得到的指定值超出范围?

问题描述 投票:0回答:1

我编写了此函数以循环遍历vba宏中的PowerPoint幻灯片。我希望它随后遍历每张幻灯片上的形状并将文本设置为用户定义的默认值。

我完成了此工作,现在由于某种原因,整理后停止工作。我收到运行时错误'-2147024809(80070057)指定的值超出范围。

当我调试时,它可以工作到特定的幻灯片。就我而言,这是一张测试幻灯片,其中包含5种不同类型的对象,并带有文本。其中有一组。

尽管进行了一些学习和培训,但我感到很困惑。非常感谢您的帮助。我敢肯定这是一个简单的解决方案,但我看不到我在做什么错。

Sub FontDefaultAllSlidesBody()
'Sets the text for all shapes on all slides in active presentation

'Set variables for functions
    Dim oSl As Slide
    Dim oSls As Slides
    Set oSls = ActivePresentation.Slides

'Set our default font settings
    For Each oSl In oSls
        For i = 1 To oSl.Shapes.Count
            With oSl.Shapes(i).TextFrame.TextRange.Font
                .Size = 16
                .Bold = msoFalse
                .Italic = msoFalse
                .Underline = msoFalse
                .Color = msoThemeColorAccent1
                .Name = "+mn-lt"
            End With
        Next i
     Next oSl
End Sub
vba powerpoint powerpoint-vba
1个回答
0
投票

这可能足以解决问题。它会测试每个形状,以查看它是否[[can包含文本,如果包含,是否does包含文本,然后才尝试对文本进行[[modify。

我还稍微修改了循环以使用Shape对象;减少了很多打字工作,而且(我认为)使您处理的内容更加清晰。Sub FontDefaultAllSlidesBody() 'Sets the text for all shapes on all slides in active presentation 'Set variables for functions Dim oSl As Slide Dim oSls As Slides ' This will make it easier to read: Dim oSh as Shape Set oSls = ActivePresentation.Slides 'Set our default font settings For Each oSl In oSls For Each oSh in oSl.Shapes ' Add this to keep it from touching shapes that ' can't contain text If oSh.HasTextFrame Then ' And skip shapes that have no text If oSh.TextFrame.HasText Then With oSh.TextFrame.TextRange.Font .Size = 16 .Bold = msoFalse .Italic = msoFalse .Underline = msoFalse .Color = msoThemeColorAccent1 .Name = "+mn-lt" End With End If ' HasText End If ' HasTextFrame Next ' Shape Next oSl End Sub
© www.soinside.com 2019 - 2024. All rights reserved.