vba控制图像透明度可能吗?

时间:2016-12-19 13:15:39

标签: excel image vba transparency

我的工作表中有一个想要淡出的图像。 要做到这一点,我想找到一种方法为图像设置不同的透明度,如下所示:

Set myPicture = ActiveSheet.Pictures.Insert(pic)

With myPicture
.Transparency = 0.5
Application.Wait (Now + TimeValue("00:00:01"))
.Transparency = 0.3
Application.Wait (Now + TimeValue("00:00:01"))
.Transparency = 0.1
Application.Wait (Now + TimeValue("00:00:01"))
.Delete
End With

目前我得到一个不受支持的对象错误消息让我相信这可能是不可能的。

如果可能,请somoene告诉我该怎么做?感谢

1 个答案:

答案 0 :(得分:2)

我花了很长时间才开始工作(直到我尝试了DoEvents

Sub FadeInFadeOut()
    Dim r As Range
    Set r = Selection
    ActiveSheet.Shapes("Rectangle 1").Select
    Selection.ShapeRange.Fill.Transparency = 1

    For i = 1 To 100
        Selection.ShapeRange.Fill.Transparency = 1 - i / 100
        DoEvents
    Next

    For i = 1 To 100
        Selection.ShapeRange.Fill.Transparency = i / 100
        DoEvents
    Next

    r.Select
End Sub

它适用于我在工作表上放置的自选图形。

<强> 注:

您必须调整 100 以调整淡入/淡出速度。

修改#1:

以下是一些垃圾代码(基于记录器),用于在工作表上删除自选图形并用图片填充:

Sub PicturePlacer()
    Dim sh As Shape

    ActiveSheet.Shapes.AddShape(msoShapeRectangle, 312.75, 176.25, 266.25, 129.75). _
        Select

    Selection.Name = "Sargon"

    Application.CommandBars("AutoShapes").Visible = False
    Range("G4").Select
    ActiveCell.FormulaR1C1 = "123"
    Range("G5").Select
    ActiveSheet.Shapes("Sargon").Select
    Selection.ShapeRange.Fill.Transparency = 0.56
    Selection.ShapeRange.Line.Weight = 0.75
    Selection.ShapeRange.Line.DashStyle = msoLineSolid
    Selection.ShapeRange.Line.Style = msoLineSingle
    Selection.ShapeRange.Line.Transparency = 0#
    Selection.ShapeRange.Line.Visible = msoTrue
    Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
    Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
    Selection.ShapeRange.Fill.Visible = msoTrue
    Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
    Selection.ShapeRange.Fill.BackColor.RGB = RGB(255, 255, 255)
    Selection.ShapeRange.Fill.UserPicture "C:\Users\garys\Pictures\babies.jpeg"
End Sub

记住命名形状并在引用该形状的所有代码中使用该名称。