首页 > 解决方案 > Excel VBA 图片插入不起作用 - 仅适用于某些 URL

问题描述

我使用下面的 VBA 代码从图像 URL 插入图片。出于某种原因,此代码适用于某些图像 URL,但不适用于其他 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

标签: excelvbaexcel-formula

解决方案


推荐阅读