ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 6209|回复: 10

[求助] 请问如何用VBA更改动画文本按字母延时百分比

[复制链接]

TA的精华主题

TA的得分主题

发表于 2011-3-3 08:20 | 显示全部楼层 |阅读模式
在没有调整的情况下,vba生成的动画的字母延时是100延时,我希望只有10延时。请问VBA中应该怎么写呢?
因为希望动画自行添加并自行播放,所以使用了ANIMATIONSETTINGS,而没有使用addeffect。现在的vba代码如下:
Sub a1()
Dim doc As Slide, a As Variant, shp As Shape, text1$,  shp_t As Variant
Set doc = ActivePresentation.Slides(1)
For Each a In doc.Shapes
a.Delete
Next
Set shp = doc.Shapes.AddShape(msoShapeRoundedRectangle, 10, 25, 200,100)
shp.Name = "3101"
shp.Line.Visible = msoFalse
shp.Fill.ForeColor.RGB = RGB(255, 255, 255)
text1 = "3101 B2051"
Set shp_t = shp.TextFrame2.TextRange
shp_t.Text = text1
shp_t.Font.Size = 24
shp_t.Font.Name = "Verdana"

With shp.AnimationSettings
    .AdvanceMode = ppAdvanceOnTime
    .AdvanceTime = 0
    .EntryEffect = ppEffectSwivel
    .TextLevelEffect = ppAnimateByFirstLevel
    .TextUnitEffect = ppAnimateByCharacter
    .Animate = msoTrue
End With
End Sub
无标题.jpg

TA的精华主题

TA的得分主题

发表于 2011-3-3 09:53 | 显示全部楼层
动画效果很不错,但要正常运行好象要修改Set shp_t = shp.TextFrame2.TextRange
Sub a1()
Dim doc As Slide, a As Variant, shp As Shape, text1$,  shp_t As Variant
Set doc = ActivePresentation.Slides(1)
For Each a In doc.Shapes
a.Delete
Next
Set shp = doc.Shapes.AddShape(msoShapeRoundedRectangle, 10, 25, 200,100)
shp.Name = "3101"
shp.Line.Visible = msoFalse
shp.Fill.ForeColor.RGB = RGB(255, 255, 255)
text1 = "3101 B2051"

Set shp_t = shp.TextFrame.TextRange

shp_t.Text = text1
shp_t.Font.Size = 24
shp_t.Font.Name = "Verdana"

With shp.AnimationSettings
    .AdvanceMode = ppAdvanceOnTime
    .AdvanceTime = 0
    .EntryEffect = ppEffectSwivel
    .TextLevelEffect = ppAnimateByFirstLevel
    .TextUnitEffect = ppAnimateByCharacter
    .Animate = msoTrue
End With
End Sub

[ 本帖最后由 chuhaiou 于 2011-3-3 09:59 编辑 ]

TA的精华主题

TA的得分主题

 楼主| 发表于 2011-3-3 10:32 | 显示全部楼层
可能2010的代码和以前的有点不同吧。
继续求助中……

TA的精华主题

TA的得分主题

发表于 2011-3-3 13:31 | 显示全部楼层

回复 3楼 gostnort 的帖子

变通处理啊,vba中如果实在找不出字符延时的属性或方法的话,可以通过改变动画的速度来间接实现。我改写你这个代码如下:

Sub charEff()
    Dim iSld As Slide, allShp As Shape, newShp As Shape
    Randomize
    Set iSld = ActiveWindow.Selection.SlideRange(1)
    For i = iSld.Shapes.Count To 1 Step -1
        iSld.Shapes(i).Delete
    Next
    Set newShp = iSld.Shapes.AddShape(msoShapeRoundedRectangle, 10, 25, 200, 100)
    With newShp
        .Name = "3101"
        .Line.Visible = msoFalse
        .Fill.ForeColor.SchemeColor = ppForeground
        .Fill.Visible = msoFalse
        With .TextFrame.TextRange
            .Text = "3101 B2051"
            .Font.Size = 24
            .Font.Name = "Verdana"
            For i = 1 To Len(.Text)
                .Characters(i, 1).Font.Color.RGB = 16777216 * Rnd
            Next
        End With
    End With
    With newShp.AnimationSettings
        .AdvanceMode = ppAdvanceOnTime
        .AdvanceTime = 0
        .EntryEffect = ppEffectSwivel
        .TextLevelEffect = ppAnimateByFirstLevel
        .TextUnitEffect = ppAnimateByCharacter
        .Animate = msoTrue
    End With
    With iSld.TimeLine.MainSequence(1).Timing
        .Duration = 0.25
        .RepeatCount = 9999
        .Decelerate = 0.1
    End With
End Sub

[ 本帖最后由 laose 于 2011-3-3 13:45 编辑 ]

TA的精华主题

TA的得分主题

 楼主| 发表于 2011-3-3 14:02 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
感谢3楼的提点。在3楼的基础上做了些实验。虽然可以通过缩短时间来完成在固定时间内播放完动画。但不能做到前一个字母动画还没有完成的时候,下一个就已经开始。那种连续的变化和跳动的感觉似乎只能改变那个数字才可以。

TA的精华主题

TA的得分主题

发表于 2011-3-3 14:13 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

回复 5楼 gostnort 的帖子

是的。手工修改下岂不是很简单。vba动画和常规动画在实现手段上不尽相同,取长补短吧。

TA的精华主题

TA的得分主题

 楼主| 发表于 2011-3-3 15:08 | 显示全部楼层
可惜。这本来是要写在EXCEL中,让EXCEL来控制PPT的。如今只能打折扣了。
感谢各位的帮助。现给出完整的代码。
另:本来想实现彩虹边框的,实在不会,只能转向用彩虹字了。电脑上只有PPT2010,PPT03的同志们需要稍微按照楼上的提示做一点修改。
Private r%, g%, bl%

Sub a1()
Dim sld As Slide, a As Variant, shp As Shape, text1$, n%, shp_t As Variant, h%
Set sld = ActivePresentation.Slides(1)
For Each a In sld.Shapes
a.Delete
Next
Set shp = sld.Shapes.AddShape(msoShapeRoundedRectangle, 10, 25, 200, 100)
shp.Visible = msoTrue
shp.Name = "3101"
shp.Line.Visible = msoFalse
shp.Fill.ForeColor.RGB = RGB(255, 255, 255)
text1 = "3101 B2051" & vbCrLf & "08:00  201"
Set shp_t = shp.TextFrame2.TextRange
shp_t.Text = text1
shp_t.Font.Size = 24
shp_t.Font.Name = "Verdana"
For n = 1 To Len(text1) - 1
    If shp_t.Characters(n, 1) <> " " Then
        h = 360 / Len(text1) * n
        Call hsb2rgb(h, 0.8, 0.8)
        shp_t.Characters(n, 1).Font.Fill.ForeColor.RGB = RGB(r, g, bl)
    End If
Next

With shp.AnimationSettings
    .EntryEffect = ppEffectSwivel
    .AdvanceMode = ppAdvanceOnTime
    .AdvanceTime = 0
    .TextUnitEffect = ppAnimateByCharacter
    .Animate = msoTrue
End With
With sld.TimeLine.MainSequence(1).Timing
.Duration = 1.5
.Speed = 10
End With
End Sub

Sub hsb2rgb(h As Integer, s As Single, br As Single)
Dim hi%, p!, q!, t!, f!, v!
v = br
If v = 0 Then r = g = b = 0: Exit Sub
If s = 0 Then r = g = b = v * 255: Exit Sub

    hi = Int(h / 60)
    f = h / 60 - hi
    p = v * (1 - s)
    q = v * (1 - s * f)
    t = v * (1 - s * (1 - f))
    Select Case hi
    Case 0
        r = v * 255: g = t * 255: bl = p * 255
    Case 1
        r = q * 255: g = v * 255: bl = p * 255
    Case 2
        r = p * 255: g = v * 255: bl = t * 255
    Case 3
        r = p * 255: g = q * 255: bl = v * 255
    Case 4
        r = t * 255: g = p * 255: bl = v * 255
    Case 5
        r = v * 255: g = p * 255: bl = q * 255
    End Select
End Sub

TA的精华主题

TA的得分主题

发表于 2011-3-3 22:25 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
学习下,PPT刚接触

TA的精华主题

TA的得分主题

发表于 2011-3-4 12:07 | 显示全部楼层

回复 7楼 gostnort 的帖子

终于可以实现了:

Private r As Integer, g As Integer, b1 As Integer

Sub charEff()
    Dim iSld As Slide, allShp As Shape, newShp As Shape, i As Integer, h As Integer
    Set iSld = ActiveWindow.Selection.SlideRange(1)
    For i = iSld.Shapes.Count To 1 Step -1
        iSld.Shapes(i).Delete
    Next
    Set newShp = iSld.Shapes.AddShape(msoShapeRoundedRectangle, 10, 25, 200, 100)
    With newShp
        .Name = "txtShp"
        .Line.Visible = msoFalse
        .Fill.Visible = msoFalse
        With .TextFrame.TextRange
            .Text = "3101 B2051" & vbCrLf & "08:00  201"
            .Font.Size = 24
            .Font.Name = "Verdana"
            For i = 1 To Len(.Text)
                If .Characters(i, 1) <> " " Then
                    h = 360 / Len(.Text) * i
                    Call hsb2rgb(h, 0.8, 0.8)
                    .Characters(i, 1).Font.Color.RGB = RGB(r, g, b1)
                End If
            Next
        End With
    End With
    With iSld.TimeLine.MainSequence
        .ConvertToTextUnitEffect .AddEffect(newShp, msoAnimEffectSwivel), msoAnimTextUnitEffectByCharacter
        .Item(1).Timing.Duration = 1
    End With
End Sub

Sub hsb2rgb(h As Integer, s As Single, br As Single)
    Dim hi As Integer, p As Single, q As Single, t As Single, f As Single, v As Single
    v = br
    If v = 0 Then r = g = b = 0: Exit Sub
    If s = 0 Then r = g = b = v * 255: Exit Sub

    hi = Int(h / 60)
    f = h / 60 - hi
    p = v * (1 - s)
    q = v * (1 - s * f)
    t = v * (1 - s * (1 - f))
    Select Case hi
    Case 0
        r = v * 255: g = t * 255: bl = p * 255
    Case 1
        r = q * 255: g = v * 255: bl = p * 255
    Case 2
        r = p * 255: g = v * 255: bl = t * 255
    Case 3
        r = p * 255: g = q * 255: bl = v * 255
    Case 4
        r = t * 255: g = p * 255: bl = v * 255
    Case 5
        r = v * 255: g = p * 255: bl = q * 255
    End Select
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2011-3-6 00:30 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
非常感谢9楼的解答!一直以为duration只控制整个对象的动画长度,殊不知原来定义为字母动画后就是定义每个字母的动画长度了。
在此基础上,因为ADDEFFECT不能自动运行,需要加入一个隐形的形状引导需要显示的动画。隐形形状的动画设为自动,而字母旋转动画跟随上一个动画播放。
在此还要感谢9楼的解答。因为FOR EACH循环好像不能删除没有线条和填充的形状,只能用FOR NEXT删除。
但是我个人不喜欢在循环内调用对象,而比较喜欢使用简单的变量以增加速度。
不过9楼的变量定义有误,不知道是不是因为开头没有Option Explicit呢。
改进后的完整代码如下:
Option Explicit

Private r As Integer, g As Integer, bl As Integer

Sub charEff()
Dim Sld As Slide, Shp As Shape, shp_tmp As Shape, h As Integer, text1$, text1_len As Byte, n%
Set Sld = ActiveWindow.Selection.SlideRange(1)
For n = Sld.Shapes.Count To 1 Step -1 '删除页面上的所有形状
        Sld.Shapes(n).Delete
Next
Set Shp = Sld.Shapes.AddShape(msoShapeRoundedRectangle, 10, 25, 200, 100) '主要形状
Set shp_tmp = Sld.Shapes.AddShape(msoShapeRoundedRectangle, 4, 4, 4, 4) '需隐形的形状
With shp_tmp
    .Line.Visible = msoFalse '让形状不可见,用shp_tmp.visible=msofalse会导致动画添加的失败
    .Fill.Visible = msoFalse
End With
With Shp
        .Name = "txtShp"
        .Line.Visible = msoFalse
        .Fill.Visible = msoFalse
        With .TextFrame.TextRange
            text1 = "3101 B2051" & vbCrLf & "08:00  201"
            text1_len = Len(text1) '把字符串长度写入变量
            .text = text1
            .Font.Size = 24
            .Font.Name = "Verdana"
            For n = 1 To text1_len
                If .Characters(n, 1) <> " " Then
                    h = 360 / text1_len * n
                    Call hsb2rgb(h, 0.8, 0.8)
                    .Characters(n, 1).Font.Color.RGB = RGB(r, g, bl)
                End If
            Next
        End With
End With

With shp_tmp.AnimationSettings '为隐藏形状添加自动动画
    .EntryEffect = ppEffectAppear
    .AdvanceMode = ppAdvanceOnTime
    .AdvanceTime = 0
    .Animate = msoTrue
End With
With Sld.TimeLine.MainSequence '为主形状添加字母动画
    .ConvertToTextUnitEffect .AddEffect(Shp, msoAnimEffectSwivel), msoAnimTextUnitEffectByCharacter '添加字母动画
    .Item(2).Timing.Duration = 1 '如果程序将添加大量形状执行大量动画的时候,item(X)还需要另外求呢。
    .Item(2).Timing.TriggerType = msoAnimTriggerAfterPrevious '设为跟随上一个动画播放
End With
End Sub

Sub hsb2rgb(h As Integer, s As Single, br As Single)
    Dim hi As Integer, p As Single, q As Single, t As Single, f As Single, v As Single
    v = br
    If v = 0 Then r = g = bl = 0: Exit Sub
    If s = 0 Then r = g = bl = v * 255: Exit Sub

    hi = Int(h / 60)
    f = h / 60 - hi
    p = v * (1 - s)
    q = v * (1 - s * f)
    t = v * (1 - s * (1 - f))
    Select Case hi
    Case 0
        r = v * 255: g = t * 255: bl = p * 255
    Case 1
        r = q * 255: g = v * 255: bl = p * 255
    Case 2
        r = p * 255: g = v * 255: bl = t * 255
    Case 3
        r = p * 255: g = q * 255: bl = v * 255
    Case 4
        r = t * 255: g = p * 255: bl = v * 255
    Case 5
        r = v * 255: g = p * 255: bl = q * 255
    End Select
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-24 13:43 , Processed in 0.042310 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表