如何使用 VBA 在 Excel 2016 中更改插入图片(图标)的填充颜色

How to change fill color for inserted picture (icon) in Excel 2016 with VBA

尝试添加 'Icon'('Insert' 选项卡下的那个 - 不是条件格式)并更改填充颜色。使用宏记录器会产生以下代码:

Sub Macro1()

    ActiveSheet.Pictures.Insert( _
        "https://hubblecontent.osi.office.net/ContentSVC/Content/Download?provider=MicrosoftIcon&fileName=Document.svg" _
    ).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent1 'this line throws the error
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.25
        .Transparency = 0
        .Solid
    End With

End Sub

这是意料之中的,因为它与用于更改 'Shape' 的填充颜色的代码相同。问题是代码实际上不起作用。它抛出 运行 时间错误 The specified value is out of range

在尝试弄清楚时,我注意到当右键单击插入的 'Icon' 时 'Fill' 选项被禁用,手动插入时显然不是这种情况。我怀疑它与 'Pictures' 对象有关,而不是 'Shapes' and/or 需要从互联网上提取图像信息,但我不是专家。这就是我来这里的原因。

我想知道这是否可以通过 VBA 实现,或者我是否应该采取不同的路线?

试试这个:

Sub Change_Hand_to_Yellow()
Dim shp1 As Shape
Set shp1 = ActiveSheet.Shapes("Graphic 12")
With shp1
 .Fill.ForeColor.RGB = vbYellow
End With

End Sub

我意识到这回答了一个老问题。我 运行 遇到了同样的问题,显然录制的宏没有太大帮助,所以我不得不做一些修补。这对我有用...

Sub Macro1()

    Const path As String _
    = "https://hubblecontent.osi.office.net/ContentSVC/Content/Download?provider=MicrosoftIcon&fileName=Document.svg"

    Dim sheet As Worksheet
    Set sheet = ActiveSheet
    Dim insertedIcon As Shape
    Set insertedIcon = sheet.Shapes.AddPicture(path, msoFalse, msoTrue, 0, 0, -1, -1)

    With insertedIcon.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent1
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.25
        .Transparency = 0
        .Solid
    End With

End Sub