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

时间:2017-01-28 04:26:18

标签: excel-vba office365 excel-2016 vba excel

尝试添加“图标”(“插入”标签下的图标 - 而不是条件格式化图标)并更改填充颜色。使用宏录制器会产生以下代码:

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

这是预期的,因为它与用于更改“形状”的填充颜色的代码相同。问题是代码实际上不起作用。它会引发运行时错误The specified value is out of range

在尝试解决这个问题时,我注意到当右键单击插入的“图标”时,“填充”选项被禁用,这显然不是手动插入时的情况。我怀疑它与'图片'对象有关,而不是'形状'和/或需要从互联网中提取图像信息,但我不是专家。这就是我在这里的原因。

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

2 个答案:

答案 0 :(得分:0)

尝试一下:

var john = new Object():
john.father = "raja";  //1st way to assign using dot operator
john["mother"] = "rani";// 2nd way to assign using brackets and key must be string

答案 1 :(得分:0)

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

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