Excel VBA图片插入不起作用-仅适用于某些URL

时间:2018-11-07 20:07:31

标签: excel vba excel-vba excel-formula

我已使用下面的VBA代码从图像URL插入图片。 由于某些原因,此代码可用于某些图像URL,但不适用于其他图像。

例如,以下网址可以使用

http://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/3123076.png&w=350&h=254

https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/David_Njoku_2018.jpg/220px-David_Njoku_2018.jpg

但以下内容不会

http://ecx.images-amazon.com/images/I/41HkOMYqc9L.SL500.jpg

http://ecx.images-amazon.com/images/I/41gWzC%2B5IqL.SL500.jpg

http://ecx.images-amazon.com/images/I/41gWzC%2B5IqL.SL500.jpg

有人知道“ ActiveSheet.Pictures.Insert”命令是否有任何限制?

下面是我正在使用的代码

Sub URLPictureInsert()
    Dim Pshp As Shape
    Dim xRg As Range
    Dim xCol As Long
    On Error Resume Next
    Application.ScreenUpdating = False
    Set Rng = ActiveSheet.Range("A1 :A24")
    For Each cell In Rng
        filenam = cell
        ActiveSheet.Pictures.Insert(filenam).Select
        Set Pshp = Selection.ShapeRange.Item(1)
        If Pshp Is Nothing Then GoTo lab
        xCol = cell.Column + 1
        Set xRg = Cells(cell.Row, xCol)
        With Pshp
            .LockAspectRatio = msoFalse
            .Width = 100
            .Height = 100
            .Top = xRg.Top + (xRg.Height - .Height) / 2
            .Left = xRg.Left + (xRg.Width - .Width) / 2
        End With
lab:
    Set Pshp = Nothing
    Range("A1").Select
    Next
    Application.ScreenUpdating = True
End Sub

0 个答案:

没有答案