如何将 URL(在工作表中)转换为图像

How do I convert URL (in a worksheet) to an image

我有这段代码可以将 B 列中的一组 URL 转换为 C 列中的图像,但出现错误:

Unable to get the Insert property of the Pictures class. My code :

Private Sub Insert_Pic()

Dim pic As String
Dim myPicture As Picture
Dim rng As Range
Dim item As Range

lRow = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row

Set rng = Range("B3:B" & lRow)
    For Each item In rng
        pic = item.Offset(0, -1)
        If pic = "" Then Exit Sub
            Set myPicture = ActiveSheet.Pictures.Insert(pic)
            With myPicture
                .ShapeRange.LockAspectRatio = msoFalse
                .Width = item.Width
                .Height = item.Height
                .Top = Rows(item.Row).Top
                .Left = Columns(item.Column).Left
                .Placement = xlMoveAndSize
            End With
    Next

End Sub

感谢您的帮助

调试中的算法是从一些微小的、有效的东西开始,然后继续。

作为开始 - 仅使用这 4 行,运行 它们:

Sub TestMe()

    Dim myPicAddress As String
    myPicAddress = "https://www.vitoshacademy.com/wp-content/uploads/2016/02/va2.png"
    Dim myPic As Picture
    Set myPic = ActiveSheet.Pictures.Insert(myPicAddress)
    
End Sub

然后,开始处理您的代码,将 With-End With 部分添加到已经运行的代码中:

Sub TestMe2()

    Dim myPicAddress As String
    myPicAddress = "https://www.vitoshacademy.com/wp-content/uploads/2016/02/va2.png"
    Dim myPicture As Picture
    Set myPicture = ActiveSheet.Pictures.Insert(myPicAddress)
    
    Dim item As Range
    Set item = ActiveSheet.Cells(5, 5)
    
    With myPicture
        .ShapeRange.LockAspectRatio = msoFalse
        .Width = item.Width
        .Height = item.Height
        .Top = Rows(item.Row).Top
        .Left = Columns(item.Column).Left
        .Placement = xlMoveAndSize
    End With

End Sub

最后,看一下循环以及作为图片字符串传递的内容。错误可能隐藏在那里。